C++ - 不是类或命名空间错误

标签 c++

我是 C++ 的新手,当我尝试调用 create_event_file() 函数时,出现以下错误“fw”不是主文件中的类或命名空间。

这是我的代码。

#ifndef FILE_WRITER_H
#define FILE_WRITER_H
#include <string>
using namespace std;

class File_Writer{

private:
int test;

public:
File_Writer() { }

void create_event_file(void);
void write(const string file_name, string data);
};

#endif // FILE_WRITER_H

cpp文件

#include "file_writer.h"
#include <iostream>
#include <fstream>
using namespace std;

File_Writer::File_Writer(void){
cout << "Object of File_Writer is created" << endl;
}

void File_Writer::create_event_file(void){

ofstream outputFile;

outputFile.open("event.txt");

string data;
cout << "Enter event title : " << endl;
getline(cin,data);
outputFile << textToSave;

cout << "Enter event date : " << endl;
getline(cin,data);
outputFile << textToSave;

cout << "Enter event start time : " << endl;
getline(cin,data);
outputFile << textToSave;

outputFile.close();
}

void File_Writer::write(const string file_name, string data){

ofstream outputFile;

outputFile.open("all.txt");

outputFile << data;

outputFile.close();
}

和主文件

#include <iostream>
#include "file_writer.h"
using namespace std;


int main(){

string input;
File_Writer fw;

cout << "Welcome to the event creation program!\n" << endl;
cout << "---------------------------" << endl;
cout << "| event - To create event file   |" << endl;
cout << "| entrants - To create entrants file |" << endl;
cout << "| coursess - To create courses file |" << endl;
cout << "---------------------------\n" << endl;

getline(cin,input);

if(input == "event")
    fw::create_event_file();


}

提前致谢

最佳答案

替换它,这意味着 fw 是类或命名空间的名称:

 fw::create_event_file();
// ^^ This is a "scope opearator"

有了这个,这意味着 fw 是一个变量:

 fw.create_event_file();
// ^ This is a "member access opearator"

关于C++ - 不是类或命名空间错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15530354/

相关文章:

c++ - 如何清除数组中的元素?

c++ - 如何用 1 个变量替换正则表达式字符串?

c++ - Boost::多精度和cardinal_cubic_b_spline

c++ - 如何可视化 OpenCV 中的内在/外在相机参数?

c++ - 不清楚的 typedef 类型

C++ 设计容器和管理列表返回

c++ - boost 单元解析字符串流

c++ - Qt,使用OpenGL,QGLWidget是私有(private)的

c++ - 为什么这个程序没有崩溃?

C++ STL 容器