0
#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

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by ravenous
0

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

0

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) {}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.