Headers and Include

I read the "Headers and Includes" article and wanted to try it out, but my compiler gives me an error, and I have no idea why. I use Code::Blocks and the GNU GCC Compiler.

All my files are in the same folder

1
2
3
4
5
6
7
8
9
10
11
12
//myclass.h
#ifndef MYCLASS_H_INCLUDED
#define MYCLASS_H_INCLUDED

class MyClass
{
public:
  void foo();
  int bar;
};

#endif // MYCLASS_H_INCLUDED 

next one
1
2
3
4
5
6
7
//myclass.cpp
#include "myclass.h"

void MyClass::foo()
{
}
int main(){}

I tried to do it without the main-function like in the article, but i get the error "undefined refrence to WinMain@16"
and the last piece of code
1
2
3
4
5
6
7
8
9
//main.cpp
#include "myclass.h"  // defines MyClass

int main()
{
  MyClass a; // no longer produces an error, because MyClass is defined
  a.foo();
  return 0;
}


Now i compiled the myclass.cpp but when compiling main.cpp it gives me "undefined refrence to Myclass::foo()"

I have no Idea what's going on.
closed account (3qX21hU5)
You can only have one entry point in your program (Only one int main()).

Delete int main inside your myclass.cpp
Now myclass.cpp does not compile "undefined refrence to WinMain@16" and the main.cpp gives me the same error.
Do you think it's maybe a code::blocks problem? Should I go to the CB Forum?
Maybe because you have no constructor for your class and you are trying to construct one on line 6 in your main function.
You set up your project incorrectly.

You have it set up to be a Win32 application instead of a console application. As a result, it's looking for "WinMain" as the entry point instead of "main".

Go in your project settings and find out where that option is (I don't remember where it is in C::B) -- it should be like a drop box with several options, one of which being "Console application" or something to that effect.

If you change that setting to be a console app, the original code you posted should work just fine.

Either that... or scrap this project entirely, start a new one, and make sure you choose the console option in the setup wizard.

Or if you don't want a console app, get rid of main() entirely and replace it with WinMain:

1
2
3
4
5
6
int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmd, int show)
{
   // program starts here -- but be warned there is no console

   return 0;
}
Last edited on
giblit wrote:
Maybe because you have no constructor for your class and you are trying to construct one on line 6 in your main function.


The compiler creates a default constructor and uses it when no constructor is provided - so it shouldn't be a source of error in this particular situation.

Confusingly however, this is an error:

MyClass a();

because there is no MyClass() constructor.

Having said that, IMO it is usually a good idea to provide a constructor - one normally needs to initialise variables with an initialiser list.
I just want to reiterate that this has nothing to do with the constructor or the class, or his header.

This is strictly a problem with the entry point.

@Roundthecorner: Your code is fine. You just need to tell C::B that you are making a console program and not a WinAPI program. Right now it thinks you are doing the latter which is why you're getting the error.
Did not know that they create a constructor by default if non provided. I always had a constructor because I find it odd to create an object without actually creating one seems to me like he is just using it as a container or something where a namespace might be more handy.
Thank you all very much (especially Disch). Works like a charm now. =) It's time to start my very first bigger project^^
Topic archived. No new replies allowed.