我正在尝试运行以下c ++文件,但是在编译时遇到了一些错误。我在MacOS上使用VSCode,并尝试进行搜索,但是我仍然被卡住,不知道是什么问题。
File.cpp文件:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
namespace sdds{
FILE* fptr;
// opens the data file and returns true is successful
bool openFile(const char filename[]) {
fptr = fopen(filename, "r");
return fptr != NULL;
}
// closes the data file
void closeFile() {
if (fptr) fclose(fptr);
}
// reads the title of the movie from the global fptr File pointer
// returns true if successful
bool readTitle(char title[]) {
return fscanf(fptr, "%[^(](", title) == 1;
}
// reads the year of the movie from the global fptr File pointer
// returns true if successful
bool readYear(int* year) {
return fscanf(fptr, "%d)", year) == 1;
}
// reads the year of the movie from the global fptr File pointer
// returns true if successful
bool readMovieRating(char rating[]) {
return fscanf(fptr, " %[^|]|", rating) == 1;
}
// reads the duration of the movie from the global fptr File pointer
// returns true if successful
bool readDuration(int* duration) {
return fscanf(fptr, "%d|", duration) == 1;
}
// reads the genras of the movie from the global fptr File pointer
// returns true if successful
bool readGenres(char genre[][11]) {
char genres[256];
int gi = 0; //genres index;
int i = 0;// genre[i] index
int j = 0;// genre[i][j] index
bool res = fscanf(fptr, " %[^|]|", genres);
if (res) { //spreading csv to array of strings
res = false;
while (genres[gi]) {
if (genres[gi] != ',') {
genre[i][j++] = genres[gi++];
res = true;
}
else {
genre[i][j] = '\0';
i++;
gi++;
j = 0;
}
}
}
genre[i++][j] = '\0';
genre[i][0] = '\0'; // NULL terminating the array of strings
return res;
}
// reads the consumerRating of the movie from the global fptr File pointer
// returns true if successful
bool readConsumerRating(float* rating) {
return fscanf(fptr, "%f\n", rating) == 1;
}
}
当我尝试使用g ++进行编译时,出现以下错误:
clang: error: linker command failed with exit code 1 (use -v to see invocation)
当我尝试运行以下Movie.cpp文件时:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <cstdio>
#include "File.h"
#include "Movie.h"
using namespace std;
namespace sdds{
Movie movies[50];
// loads all the moves into the global array of structures.
bool loadMovies() {
Movie mv;
int mnum = 0; // number of movies read
bool ok = true;
if (openFile("movies.dat")) {
while (ok && mnum < 50) {
ok = readTitle(mv.m_title) &&
readYear(&mvx.m_year) &&
readMovieRating(mv.m_rating) &&
readDuration(&mv.m_duration) &&
readGenres(mv.m_genres) &&
readConsumerRating(&mv.m_consumerRating);
if (ok) movies[mnum++] = mv;
}
closeFile();
}
return mnum == 50;
}
// returns true is the genre arg is substring of any of the
// genres of the target of the mvp (Movie*) arg.
bool hasGenre(const Movie* mvp, const char genre[]) {
int i = 0;
bool found = false;
while (!found && mvp->m_genres[i][0]) {
if (strstr(mvp->m_genres[i++], genre)) {
found = true;
}
}
return found;
}
// displays the movie info
void displayMovie(const Movie* mvp) {
int i = 1;
cout << mvp->m_title << " [" << mvp->m_year << "], "
<< mvp->m_rating << ", duration: " << mvp->m_duration << " minutes, Rating: " << mvp->m_consumerRating << "/10" << endl << " (" << mvp->m_genres[0];
while (mvp->m_genres[i][0]) {
cout << ", " << mvp->m_genres[i++];
}
cout << ")" << endl;
}
// displays all the movies containing the genre arg
void displayMoviesWithGenre(const char genre[]) {
int i, j;
for (i = 0, j = 1; i < 50; i++) {
if (hasGenre(&movies[i], genre)) {
cout << j++ << "- ";
displayMovie(&movies[i]);
}
}
if (j == 1) {
cout << "No match found for: " << genre << endl;
}
}
}
我收到以下错误:
Movie.cpp:23:8: error: use of undeclared identifier 'openFile'
if (openFile("movies.dat")) {
^
Movie.cpp:25:15: error: use of undeclared identifier 'readTitle'
ok = readTitle(mv.m_title) &&
^
Movie.cpp:26:13: error: use of undeclared identifier 'readYear'
readYear(&mv.m_year) &&
^
Movie.cpp:27:13: error: use of undeclared identifier 'readMovieRating'
readMovieRating(mv.m_rating) &&
^
Movie.cpp:28:13: error: use of undeclared identifier 'readDuration'
readDuration(&mv.m_duration) &&
^
Movie.cpp:29:13: error: use of undeclared identifier 'readGenres'
readGenres(mv.m_genres) &&
^
Movie.cpp:30:13: error: use of undeclared identifier 'readConsumerRating'
readConsumerRating(&mv.m_consumerRating);
^
Movie.cpp:33:7: error: use of undeclared identifier 'closeFile'
closeFile();
^
8 errors generated.
g++ Movie.cpp
Movie.cpp:23:8: error: use of undeclared identifier 'openFile'
if (openFile("movies.dat")) {
^
Movie.cpp:25:15: error: use of undeclared identifier 'readTitle'
ok = readTitle(mv.m_title) &&
^
Movie.cpp:26:13: error: use of undeclared identifier 'readYear'
readYear(&mv.m_year) &&
^
Movie.cpp:27:13: error: use of undeclared identifier 'readMovieRating'
readMovieRating(mv.m_rating) &&
^
Movie.cpp:28:13: error: use of undeclared identifier 'readDuration'
readDuration(&mv.m_duration) &&
^
Movie.cpp:29:13: error: use of undeclared identifier 'readGenres'
readGenres(mv.m_genres) &&
^
Movie.cpp:30:13: error: use of undeclared identifier 'readConsumerRating'
readConsumerRating(&mv.m_consumerRating);
^
Movie.cpp:33:7: error: use of undeclared identifier 'closeFile'
closeFile();