Wald
Wald

Reputation: 1091

C++ - is not a class or namespace error

I'm fairly new to C++ and I'm getting the following error 'fw' is not a class or namespace in the main file when I try to call the create_event_file() function.

Here is my code.

#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

The cpp file

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

And the main file

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


}

Thanks in advance

Upvotes: 0

Views: 1448

Answers (1)

Drew Dormann
Drew Dormann

Reputation: 63704

Replace this, which implies that fw is the name of a class or namespace:

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

With this, which implies that fw is a variable:

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

Upvotes: 6

Related Questions