Author Topic: Problem including custom header file  (Read 7311 times)

ufo502070

  • Guest
Problem including custom header file
« on: February 24, 2011, 09:45:42 pm »
Hello. I'am a C++ Beginner.
I'am using:
  CodeBlocks 10.05
  mingw32-g++.exe as C++ Compiler.
  Windows 7

When I try to include my self made header file YourName.h into my "hello world"-Application then the Debugger says:

Quote
Compiling: main.cpp
D:\C++ Exercise\Fehler\main.cpp:4: error: expected unqualified-id before 'using'
D:\C++ Exercise\Fehler\main.cpp: In function 'int main()':
D:\C++ Exercise\Fehler\main.cpp:8: error: 'cout' was not declared in this scope
D:\C++ Exercise\Fehler\main.cpp:8: error: 'endl' was not declared in this scope

Process terminated with status 1 (0 minutes, 0 seconds)
3 errors, 0 warnings

When I remove the line #inlude "YourName.h" then the debugger will not release any error message.
I already checked many topics in other forums but didn't find anything helpful.


Here is the Code of...

main.cpp

Code
#include <iostream>
#include "YourName.h"

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}


YourName.h

Code
#ifndef YOURNAME_H_INCLUDED
#define YOURNAME_H_INCLUDED

struct Insect {

    Insect () {Legs = 6;};

    int Legs;

}

#endif // YOURNAME_H_INCLUDED


What am I doing wrong?
Even adding "std::" in front of cout or endl; won't help and cause further errors.

Please help me. Thanks
« Last Edit: February 24, 2011, 09:48:53 pm by ufo502070 »

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5495
Re: Problem including custom header file
« Reply #1 on: February 24, 2011, 09:59:52 pm »
note that this is NOT a general programmers learning forum, but a forum related on using CB.
But I will help you this once.

First : it is the compiler complaining about your code, since it is incorrect, and not the debugger.

Your mistake is that you need a ";" after the closing brace of your struct definition

Code
struct Insect
{

    Insect () : mLegs(6) {}

    int mLegs;

};

Note also the other style enhancements I did;