Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I am trying to go through C++ inheritance and I have a problem with my code. I have the base class Caine and CaineCuPete derived from it. I get an error when I declare the derived class constructor.
CaineCuPete(int nPete) : Caine(cNume,cHeight,cWeight,cAge,cColor);
Error:
cNume was not declared in its scope. cHeight was not declared in its
scope. ...
You need to take these as input parameters in your derived class constructor.
CaineCuPete(int nPete, string cNume, double cHeight,
double cWeight, double cAge, int cColor ) : Caine(cNume,cHeight,cWeight,cAge,cColor),
Pete ( nPete )
{
}
This link explains:
http://www.learncpp.com/cpp-tutorial/114-constructors-and-initialization-of-derived-classes/
You should define your variables cNume,cHeight,cWeight,cAge,cColor before the first use. A common problem is that they are defined after the point of use, not before.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
How do I pass a smart pointer into this class so that it may be pushed into.
This line:
auto alltransactions(atrans);
Declares a new variable named alltransactions, which has the same name as your member.
Your constructor should use member init list to ensure your member are initialized correctly:
struct my_class {
my_class(std::shared_ptr<std::vector<cl_order>> atrans) : transactions(atrans) {}
std::shared_ptr<std::vector<cl_order>> transactions;
};
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
Team,
This question has been asked many times, but the answers don't seem to fit my situation. Here's my VERY simple code. The line of code that is causing the problem works perfectly in the main routine; however, when I try to embed an object within an object using the exact same code the error message shows up. I suspect the cause is silly, but I'm too close and can't see it...enter image description here
Within a class you have to use the member-initialization list to initialize your member variable A in B:
class B{
public:
A one; //Declare one here
int numbluemarbles;
B(): one(100){} //initialize one here
};
In c++11 or newer you can also use the newer syntax:
class B{
public:
A one = 100.0; //C++11
int numbluemarbles;
};
For more about how you can use c++11 initialization options have a read of this: http://www.informit.com/articles/article.aspx?p=1852519
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have a friend class B of my class A,
Is it possible that B's ancesstor can also access A's functionality or this friendship is only between A and B strictly
No, friendship is not inherited. In future I suggest you try such things out before posting a question.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is from a CPP Source File:
class classname{
//constructor
classname(anotherclass *ptr);
private:
string firstname;
string lastname;
};
classname::classname(anotherclass *ptr): firstname("Nathan"), lastname("Narcovy"){
//some other definitions
}
I come from C, but I do know a bit of Object Oriented Language,
But I don't understand classname:string,string . I only remember a colon : was used for inheritance.
This is actually how the initializer list works for constructor.
I've found this tutorial which seems to explain it decently for newcomer.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
class Test {
public:
int xyz=10;
};
Why does the compilation fail in c++?
It's a new feature in C++11. compile your code with -std=c++11
You should initialize members with a constructor. See this thread: in-class initialization of non-static and non-const members for good information.
It is not a static member so initialize it in constructor.Non static member cannot be initialize without constructorAlso see this for further details about the initialization of static and non staic data