C++显示 未定义函数,我 明明定义了 的
#include<iostream>#include<string>
#include<fstream>
class StoreQuote
{
public:
std::string quote, speaker;
std::ofstream fileOutput;
StoreQuote();
~StoreQuote();
void inputQuote();
void inputSpeaker();
bool write();
};
StoreQuote::StoreQuote()
{
fileOutput.open("test.txt",std::ios::app);
}
StoreQuote::~StoreQuote()
{
fileOutput.close();
}
void StoreQuote::inputQuote()
{
std::getline(std::cin, quote);
}
void StoreQuote::inputSpeaker()
{
std::getline(std::cin,speaker);
}
bool StoreQuote::write()
{
if(fileOutput.is_open())
{
fileOutput << quote << "|" << speaker << "\n";
return true;
}
else
{
return false;
}
}
int main()
{
std::cout << "请输入一句名言!" << '\n';
quote.inputQuote();
std::cout << "请输入作者:\n";
quote.inputSpeaker();
if( quote.write() )
{
std::cout << "成功写入文件^_^";
}
else
{
std::cout << "写入文件失败T_T";
return 1;
}
return 0;
}
报的 错误是 : D:\ccc\12\1 2.cpp:55: error: `quote' was not declared in thisscope
照着小甲鱼的源代码 对了一遍自己还检查了几遍还是没发现 错误好火大, 求解 到底 哪里出问题了
还有有没有一个 错误 收集软件或什么的 看不懂 英文一行一行复制 到百度好累的说
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class StoreQuote
{
public:
string quote, speaker;
ofstream fileOutput;
StoreQuote();
~StoreQuote();
void inputQuote();
void inputSpeaker();
bool write();
};
StoreQuote::StoreQuote()
{
fileOutput.open("test.txt",std::ios::app);
}
StoreQuote::~StoreQuote()
{
fileOutput.close();
}
void StoreQuote::inputQuote()
{
getline(std::cin, quote);
}
void StoreQuote::inputSpeaker()
{
getline(cin,speaker);
}
bool StoreQuote::write()
{
if(fileOutput.is_open())
{
fileOutput << quote << "|" << speaker << "\n";
return true;
}
else
{
return false;
}
}
int main()
{
StoreQuotequote;
cout << "请输入一句名言!" << '\n';
quote.inputQuote();
cout << "请输入作者:\n";
quote.inputSpeaker();
if( quote.write() )
{
cout << "成功写入文件^_^";
}
else
{
cout << "写入文件失败T_T";
return 1;
}
return 0;
}
其实楼上的说法是正确的。估计你是新手。给你解释一下
修改的地方 标红的两处。
1.加上using namespace std; 这条语句可以将你程序中的所有std:: 给去掉。引入标准的std命名空间。这个命名空间里面包含基本的输入输入方法。
2. 你的程序出错的主要原因跟上面第一条解释没有关系。你的程序定义了StoreQuote类,但是没有定义quote对象。类只是告诉编译器有这么个类型,但是没有具体的事例,在内存中是不会分配存储空间的。需要先定义对象,才能使用类中的所有方法。在没定义对象之前,就好像你跟编译器在说quote这个对象,但是编译器就是听不懂,不知道这个是个什么东西,所以报错了。
顶一下………… 那个变量确实没定异啊 #include<iostream>
#include<string>
#include<fstream>
class StoreQuote
{
public:
std::string quote, speaker;
std::ofstream fileOutput;
StoreQuote();
~StoreQuote();
void inputQuote();
void inputSpeaker();
bool write();
};
StoreQuote::StoreQuote()
{
fileOutput.open("test.txt",std::ios::app);
}
StoreQuote::~StoreQuote()
{
fileOutput.close();
}
void StoreQuote::inputQuote()
{
std::getline(std::cin, quote);
}
void StoreQuote::inputSpeaker()
{
std::getline(std::cin,speaker);
}
bool StoreQuote::write()
{
if(fileOutput.is_open())
{
fileOutput << quote << "|" << speaker << "\n";
return true;
}
else
{
return false;
}
}
int main()
{
std::cout << "请输入一句名言!" << '\n';
StoreQuote quote;
quote.inputQuote();
std::cout << "请输入作者:\n";
quote.inputSpeaker();
if( quote.write() )
{
std::cout << "成功写入文件^_^";
}
else
{
std::cout << "写入文件失败T_T";
return 1;
}
return 0;
}
页:
[1]