PDA

View Full Version : accessing a class via main in C++


caz1805
04-17-2005, 07:03 PM
I am trying to print the details of a dog(its name and age), its a simple program everything compiles okay except when i try and access and print these members via main. Here is the small piece of code that i am having problems with.

int main()
{

errorhandling* lassie(10, "lassie");
lassie.print();

return 0;
}

I get the errors.

: error C2065: 'lassie' : undeclared identifier
: error C2296: '*' : illegal, left operand has type 'int (__cdecl *)(int,char *)'
: error C2228: left of '.print' must have class/struct/union type
errorhandling.cpp

can anybody help it would be much appreciated, i am sure there is a simple solution just cant find it.

thanks

sage45
04-17-2005, 09:41 PM
Ok, it is probably best if you post more than just the small snippet...

But here is a small example of how to access a class and it's members:

Accessing a public member function of a class from a non-member function:// In the non-member function
// Declare variables
classType className;

// Call class public function
className.publicFunction(function parameters);By the code you posted, you are declaring a pointer to an datatype declared as errorhandling... If that is your aim then what are you trying to do (hence the need for more code)

-sage-

aman
04-17-2005, 09:45 PM
The solution is to start from a C++ tutorial, there are many online for you to learn from.


The errors show that 1) lassie is undeclared because the compiler doesn't know what errorhandling is. You'll probably need to include the header that defines the errorhandling class (probably errorhandling.h). And 2) you can't access member print because you initially declared lassie as a pointer, so first you need to allocate memory for it and then you need to access it's members using the arrow: lassie->print().

caz1805
04-18-2005, 02:03 AM
I have managed to fix all the problems except for one and this is:

error C2228: left of '.print' must have class/struct/union type
errorhandling.cpp

it wont accept lassie do you know the common problem for this error and the solution.

sorry and thank u

aman
04-18-2005, 05:27 AM
no. I posted the solution to the .print() problem already.

Dunna
04-18-2005, 05:18 PM
Ok, first you need to have a print function. So,

void print::YourClassNameHere()
{
cout << MemberOne << " is member one.\n";
cout << MemberTwo << " is member two.\n";
}

The error you're describing tells me you don't have a print function, and you're trying to call it. So first declare something like the code above. Make sure put the code for the print function AFTER the place where you declare the class itself or else you'll get more errors. Post your results or questions here.