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

Here is my main file:

#include<iostream>
#include<iomanip>
#include"countertype.h"

using namespace std;



void main(){

	CounterType counter = CounterType();

	counter.setCount(2);
	counter.getCount();

}

And my class code:

#include<iostream>
#include<iomanip>

using namespace std;

class CounterType{

public:
	int count;

	CounterType(){
		count = 0;
	}

	void setCount(int count){
		count = count;
		if(count<=0){
			count=0;
		}
	}

	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.

Also,

void main()

is no longer correct. We use

int main()

now.

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.
e.g.

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

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

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.

Never mind. I just corrected the count problem.

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!

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.