I'm trying to get my main function to read my class file. It doesn't compile.

Here is my main file:


using namespace std;

void main(){

	CounterType counter = CounterType();



And my class code:


using namespace std;

class CounterType{

	int count;

		count = 0;

	void setCount(int count){
		count = count;

	int getCount(){
		return count;

And my compiler errors:
fatal error C1083: Cannot open include file: 'countertype.h': No such file or directory Line 3
fatal error C1004: unexpected end-of-file found Line 27 countertype.cpp

This error would imply that your compiler cannot locate the file known as countertype.h

Is the compiler looking in the right place for it, and have you definitely called it countertype.h ?

As an aside, I note that your second file there, which I imagine to be countertype.h, contains a

using namespace std;

It is considered bad form to put this at the top level of your header file.


void main()

is no longer correct. We use

int main()


Edited 5 Years Ago by Moschops: n/a

On top of the comments made by Moschops, it seems blatantly obvious that you haven't got a file called countertype.h, you've put all of the code for your class into a file called countertype.cpp.
So you need to either rename countertype.cpp to countertype.h, or separate the code so that the declaration of the class is in a header file and the definition/implementation of your class is in a .cpp file with the same name.

The 'unexpected end of file' error is because you've forgotten to include a semicolon at the end of your class declaration.

class myclass
}; // <<<---- class declarations end with a semicolon!
This is a righteous post from the hippy community :)

I added those suggestions, it now compiles, but the getCount method still returns 0, when I set it to 2.

Edited 5 Years Ago by SolidSora: n/a

Edited: The post I was responding to was withdrawn.

How do you know it is zero? Your code contains no means of displaying the value to the user.

Edited 5 Years Ago by Moschops: The post I was responding to was withdrawn.

It's because you have a member variable in your class called 'count' and you're also using a local variable called 'count' in your setCount function.
So where you're doing:

count = count;

Because there are two variables in scope, it will only use the local one.
So the local one is assigning the value of itself to itself (if you follow me?!)
So the member variable doesn't get updated.
I recommend renaming the member variable to m_count. That way you can use:

m_count = count;

But take note that if you rename the member variable, you'd need to update your getCount function to return m_count too!

EDIT - Doh! Too late!

Edited 5 Years Ago by JasonHippy: n/a

This article has been dead for over six months. Start a new discussion instead.