c++ - 如何在这里访问 print() 函数中的类变量?
问题描述
书.h
class Book{
private:
std::string title;
std::string author;
int callNo;
public:
Book(std::string title,std::string author,int callNo);
void print();
};
书本.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: 错误: 'title' 未在此范围内声明 cout<<"Title: "<<title<<endl;
无论如何调用类变量而不更改 print() 的参数?
解决方案
由于它是 Book 的成员函数,它应该是
void Book::print(){
std::cout << "Title: " << title << std::endl;
std::cout << "Author: " << author << std::endl;
std::cout << "Call Number: " << callNo << std::endl;
}
推荐阅读
- c++ - 在 C++14 中计算一年中的小数天
- python-3.7 - Pygame:附加到列表的顺序搞砸了
- java - java.net.SocketException:尝试将对象从客户端传递到服务器时连接重置
- java - Shell 排序比较和交换计数
- c - 如何使用 execle() 为 /bin/login 传递环境变量?
- java - 在 Android 应用程序包名称中使用特殊字符
- php - Git Branch结帐后未将Laravel 5.8会话写入cookie
- json - json-xcode/swift 遇到问题
- r - R 平均值,如果基于条件(等级)
- clojure - 如何使中间件仅在`:swagger {:deprecated true}`时放置响应标头?