I am having problems with a simple file I am trying to create. The idea of the file is to use a header to read something from the file and then call this in the main.cpp

This is my Main.cpp file:

#include <iostream>
#include <string>

#include "Menu_Screen.h"

using namespace std;

int main()
{
   cout << line;
}

And this is my header file:

#ifndef MENU_SCREEN_H_INCLUDED
#define MENU_SCREEN_H_INCLUDED

#include <string>
#include <fstream>


using namespace std;
{

ifstream File ("Test.txt");

    string line;

    while (File.good())
    {
        getline(File, line);
    }

}


#endif // MENU_SCREEN_H_INCLUDED

In the header file, it claims that there is an unqualified-id on line 9 however I cannot see what is wrong with my code and when searching google, I can't find anything that I can either understand or make relevant to my code.

Any help would be greatly appreciated.

Recommended Answers

All 4 Replies

The code as written isn't valid; unlike in some languages, you cannot have a code block that isn't part of a function in C++.

Mind you, it is generally inadvisable to have code (as opposed to declarations) in a header file, but that's not the cause of the problem, per se.

So, what was this code supposed to do?

The problem is line 8 of the header file. Delete that semicolon at the end of the class name. Otherwise it looks to be ok.

>> it is generally inadvisable to have code (as opposed to declarations) in a header file
He has inline code, which is perfectly acceptable and often used in professional programs. One or two inline statements within the same method is ok, more than that should be moved to a *.cpp file and only function declaration in the header file. The reason for doing that is to reduce the overall size of the program. The compiler may duplicate all the code of inline functions every time the function appears in the program. So if a program calls foo.Example() 10 times then all the code in Example() will be duplicated 10 times. Clearly 1 or 2 statements in the inline method will not be a problem, but more than that could potentially cause unnecessary bulk. I say potentially because compilers are free to ignore inline keywords and statements if they so desire.

With all due respect, AD, I think you've misread the code (it happens to the best of us...). There is no class declaration; that's a using namespace directive. Furthermore, even were it a class declaration, there would still be code there which isn't inside of a method.

commented: you are correct :) +17

You are correct, I didn't read it well enough.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.