鱼C论坛

 找回密码
 立即注册
查看: 7182|回复: 4

C++显示 未定义函数,我 明明定义了 的

[复制链接]
发表于 2015-12-5 20:28:23 | 显示全部楼层 |阅读模式
20鱼币
  1. #include<iostream>
  2. #include<string>
  3. #include<fstream>

  4. class StoreQuote
  5. {
  6. public:
  7.                 std::string quote, speaker;
  8.                 std::ofstream fileOutput;
  9.                
  10.                 StoreQuote();
  11.                 ~StoreQuote();
  12.                
  13.                 void inputQuote();
  14.                 void inputSpeaker();
  15.                 bool write();                  
  16. };

  17. StoreQuote::StoreQuote()
  18. {
  19.         fileOutput.open("test.txt",std::ios::app);
  20. }

  21. StoreQuote::~StoreQuote()
  22. {
  23.         fileOutput.close();
  24. }

  25. void StoreQuote::inputQuote()
  26. {
  27.         std::getline(std::cin, quote);       
  28. }

  29. void StoreQuote::inputSpeaker()
  30. {
  31.         std::getline(std::cin,speaker);
  32. }

  33. bool StoreQuote::write()
  34. {
  35.         if(fileOutput.is_open())
  36.          {
  37.                  fileOutput << quote << "|" << speaker << "\n";       
  38.                  return true;
  39.          }
  40.          else
  41.          {
  42.                  return false;
  43.          }
  44. }

  45. int main()
  46. {
  47.         std::cout << "请输入一句名言!" << '\n';
  48.          quote.inputQuote();
  49.         std::cout << "请输入作者:\n";
  50.         quote.inputSpeaker();
  51.        
  52.         if( quote.write() )
  53.         {
  54.                 std::cout << "成功写入文件^_^";
  55.         }
  56.         else
  57.         {
  58.                 std::cout << "写入文件失败T_T";
  59.                 return 1;
  60.         }
  61.        
  62.         return 0;          
  63. }
复制代码


报的 错误是 :[Error] D:\ccc\12\1 2.cpp:55: error: `quote' was not declared in this  scope
  照着小甲鱼的源代码 对了一遍自己还检查了几遍还是没发现 错误  好火大,     求解 到底 哪里出问题了   
还有  有没有  一个 错误 收集软件或什么的   看不懂 英文  一行一行复制 到百度  好累的说

最佳答案

查看完整内容

#include #include #include using namespace std; class StoreQuote { public: string quote, speaker; ofstream fileOutput; StoreQuote(); ~StoreQuote(); void inputQuote(); void inputSpeaker(); bool write(); }; StoreQu ...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-12-5 20:28:24 | 显示全部楼层
#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()
{
        StoreQuote  quote;
        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这个对象,但是编译器就是听不懂,不知道这个是个什么东西,所以报错了。

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-12-5 20:47:47 | 显示全部楼层
顶一下…………
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-12-6 11:46:55 | 显示全部楼层
那个变量确实没定异啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2015-12-6 12:18:37 | 显示全部楼层
#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;         
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 05:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表