任何人都可以向我解释这段代码有什么问题以及如何修复它?这是一个更大的项目的一部分,如果你需要更多的信息,以给出一个anwer请告诉我.
我得到的错误是这样的:
g++ -c search.cpp
search.cpp: In constructor ‘Search::Search()’:
search.cpp:26:26: error: ‘callnumber’ was not declared in this scope
make: *** [search.o] Error 1
Search::Search()
{
ifstream input;
input.open("data.txt", ios::in);
if(!input.good())
//return 1;
string callnumber;
string title;
string subject;
string author;
string description;
string publisher;
string city;
string year;
string series;
string notes;
while(! getline(input, callnumber, '|').eof())
{
getline(input, title, '|');
getline(input, subject, '|');
getline(input, author, '|');
getline(input, description, '|');
getline(input, publisher, '|');
getline(input, city, '|');
getline(input, year, '|');
getline(input, series, '|');
getline(input, notes, '|');
Book *book = new Book(callnumber, title, subject, author, description, publisher, city, year, series, notes);
Add(book);
}
input.close();
}
Run Code Online (Sandbox Code Playgroud)
Set*_*gie 12
在这一行:
if(!input.good())
//return 1;
string callnumber;
Run Code Online (Sandbox Code Playgroud)
您注释掉用分号线路和你不使用大括号,当它涉及到空格和换行C++是不是空白敏感的1标记之间,所以通过取出评论(并添加缩进),我们可以看到,它相当于
if (!input.good())
string callnumber;
Run Code Online (Sandbox Code Playgroud)
我们可以看到声明callnumber是本地的if.添加大括号或分号以使声明callnumber超出if(或者只是完全删除):
if (!input.good()) { // or ;
//return 1;
}
string callnumber;
Run Code Online (Sandbox Code Playgroud)
1除宏外