what is wrong with this code?
-
Hello All ? can anyone please tell me what's wrong with following code?
class A { public: A(const string& s) { /* */} string f() { return "hello world"; } }; Class B : public A { private: string s; public: B():A(s=f()) { } }; void main() { B obB; }
Thank you,
sanket patel
-
Hello All ? can anyone please tell me what's wrong with following code?
class A { public: A(const string& s) { /* */} string f() { return "hello world"; } }; Class B : public A { private: string s; public: B():A(s=f()) { } }; void main() { B obB; }
Thank you,
sanket patel
Is this a quiz? Why don't you first tell us what problem you are having.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Is this a quiz? Why don't you first tell us what problem you are having.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
oh I am sorry! whenever I try to run this code my program crashes. I dont know what is wrong with it?
sanket patel
-
oh I am sorry! whenever I try to run this code my program crashes. I dont know what is wrong with it?
sanket patel
sanket.patel wrote:
...my program crashes.
Where? The term "crash" rarely means anything useful.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
sanket.patel wrote:
...my program crashes.
Where? The term "crash" rarely means anything useful.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
I am sorry again!! But I dont know why this code is not executing. I know there is some problem with my understanding of the code. so I just want to know that Where I am wrong? Thank you
sanket patel
-
I am sorry again!! But I dont know why this code is not executing. I know there is some problem with my understanding of the code. so I just want to know that Where I am wrong? Thank you
sanket patel
Have you tried:
class A
{
public:
A() {}
A(const string& s) {}string f() { return "hello world"; }
};
class B : public A
{
private:
string s;
public:
B()
{
s = f();
}
};
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
-
Hello All ? can anyone please tell me what's wrong with following code?
class A { public: A(const string& s) { /* */} string f() { return "hello world"; } }; Class B : public A { private: string s; public: B():A(s=f()) { } }; void main() { B obB; }
Thank you,
sanket patel
In class A, does it still crash if you make the f() method static? You are using A's method before A is ctor'ed. It looks like it shouldn't matter, but who knows, maybe some static consts are not available until after the first inst of the class is created? Sounds doubtful, doesn't it.
-
Have you tried:
class A
{
public:
A() {}
A(const string& s) {}string f() { return "hello world"; }
};
class B : public A
{
private:
string s;
public:
B()
{
s = f();
}
};
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
Thank you David!! the code you show is working fine! so can you please explain me where I was wrong ?
sanket patel
-
In class A, does it still crash if you make the f() method static? You are using A's method before A is ctor'ed. It looks like it shouldn't matter, but who knows, maybe some static consts are not available until after the first inst of the class is created? Sounds doubtful, doesn't it.
Thanks NealAB, Yeah I got your point! I think that is only happening. I was trying to use A's method before it was created. Thanks again...
sanket patel