【发布时间】:2021-03-22 07:43:23
【问题描述】:
书.h
class Book{
private:
std::string title;
std::string author;
int callNo;
public:
Book(std::string title,std::string author,int callNo);
void print();
};
Book.cpp
Book::Book(string title,string author,int callNo){
this->title=title;
this->author=author;
this->callNo=callNo;
}
void print(){
cout<<"Title: "<<title<<endl;
cout<<"Author: "<<author<<endl;
cout<<"Call Number: "<<callNo<<endl;
}
编译的时候报错:
Book.cpp:14:19:错误:“标题”未在此范围内声明 cout
有没有在不改变print()参数的情况下调用类变量?
【问题讨论】:
-
你应该写
void Book::print(){...}。 -
Book.cpp中的打印函数不是成员函数。我想你想把它改成void Book::print() { /*...*/ }