Hi there, im a c++ noobie. I have a class called node. Node has a data member id. When i try and access id in the obect named f outside of main visual studio 2003 gives me the errors:

: error C2065: 'f' : undeclared identifier
error C2228: left of '.id' must have class/struct/union type
type is ''unknown-type''

thanks in advance

have you created an object from your class?

is "f" a public.. or private class member? ;)


post ye' code and we can help more.

f is not a data member f is the name of the object im trying to create

id is public:

/////a.cpp///


#include <iostream>
#include "test.h"
using namespace std;

int fn(void)

{
   f.id = 3;

   return 0;

}


int main(void)

{
    test f;

    system("pause");

        return 0;

}


///test.cpp///

#include "test.h"

test::test()
{}

test::~test()
{}

////test.h//

class test
{

public:
    int id;
    test();
    ~test();
};

Hi there, im a c++ noobie. I have a class called node. Node has a data member id. When i try and access id in the obect named f outside of main visual studio 2003 gives me the errors:

: error C2065: 'f' : undeclared identifier
error C2228: left of '.id' must have class/struct/union type
type is ''unknown-type''

thanks in advance

You declared f in main function and not in fn function thats the problem. Declare f in fn function.

is there no way around that?

Declaring f as global but that is bad practice. I don't know what you want to do. You are not calling fn func at all.

hmm yeah there is supposed to be a call for that function. But ignore that the point is is there any way just to declare it in main and still get fn() to recognise its existance?

> and still get fn() to recognise its existance?
Pass it as a parameter.

Unless this is one of those futile exercises that some tutors seem to love (for god-knows what reason), and the answer usually involves some horridness involving macros. If this is the case, then perhaps consider a different tutor.

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.