2013-03-20 28 views
0

我对C++相当陌生,当我尝试调用create_event_file()函数时,出现以下错误'fw'不是主文件中的类或名称空间。C++ - 不是类或名称空间错误

这是我的代码。

#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(); 


} 

在此先感谢

+0

在未来这是好事,包括你的错误的行号。 (在这种情况下,这很容易,但它通常会帮助人们试图回答。) – 2013-03-20 17:23:28

+0

避免使用名称空间标准; – bitmask 2013-03-20 17:29:00

回答

6

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

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

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

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

作为一个小解释:'::'尝试访问该类的静态成员,其类型在'::'之前指定。期间访问一个变量。 – 2013-03-20 17:18:30

+0

啊......谢谢我现在觉得很笨:D – Wald 2013-03-20 17:28:29

+0

@Wald记得编译器错误会指出错误存在的特定文件和行号。你从你的问题中忽略了这一点,好像你没有注意到这一点的重要性。 – 2013-03-20 17:45:33

相关问题