Computing.Net > Forums > Programming > Simple C++ Class problem

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Simple C++ Class problem

Reply to Message Icon

Name: Photomotoz
Date: November 24, 2008 at 10:39:19 Pacific
OS: g
CPU/Ram: g
Product: g
Comment:

Hey, guys I think this is a very simple problem and that I just can't get it is due to some gap in my knowledge. Any way, what is wrong with this :

class A{
private: int value;
public : A(int v);
};

A::A(int v)
{value = v;}

//Does not work
class B {A ob(5);};
//Works
main(){A ob(5);}

By does not work, I mean compile.




Sponsored Link
Ads by Google

Response Number 1
Name: klint
Date: November 25, 2008 at 02:31:30 Pacific
Reply:

class B {A ob(5);};

That looks like you're trying to define class B, containing an A object as a private data member called ob. Is that what you're trying to do? If so, you can't initialize ob inside the class definition, because at that point you're telling the compiler what structure the class has, not what values each of its instances should contain. You need to initialize it in B's constructor.


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Simple C++ Class problem

simple C++ compile problem in Linux www.computing.net/answers/programming/simple-c-compile-problem-in-linux-/1282.html

C++ class redefinition problems www.computing.net/answers/programming/c-class-redefinition-problems/1587.html

simple c programming problem www.computing.net/answers/programming/simple-c-programming-problem/8180.html