#include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main()
{
	oneclass.hello();
	return 0;
}

class one
{
	private:
	int num;
	public:
	void hello();
}oneclass;
void one::hello()
{

num++;
cout << num;
}

Could anybody please please tell me what is wrong with this code. I think it is correct but keep getting errors:


Error 2 error C2228: left of '.hello' must have class/struct/union f:\documents and settings\j\my documents\visual studio 2008\projects\ex2\ex2\ex2.cpp 18


Error 1 error C2065: 'oneclass' : undeclared identifier f:\documents and settings\j\my documents\visual studio 2008\projects\ex2\ex2\ex2.cpp 18

Me being stupid, of course I was using object oneclass in main before declaring it!!

You probably want something more like this:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class one
{
private:
    int num;
public:
    void hello();
} oneclass;

int main()
{
    oneclass.hello();
    return 0;
}

void one::hello()
{
    num++;
    cout << num;
}

I've moved the declaration of one above main , so that the compiler knows what it is when it gets to it when compiling main .

This code will have undefined behaviour, since you don't initialise the value of num to anything. You probably want to define a constructor to initialise it for you, like this:

one::one() : num(0) {}
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.