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
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I received a code to review and I can't understand why a class cast is happening. Given the following dummy code:
class A {
int a;
void dummy() {
std::cout << "wewe";
};
};
class B {
int a;
void dummy() {
std::cout << "wewe";
};
};
A strangeCast() {
return B();
}
It builds with no errors on this project on VSCode on Linux.
I checked for any relationship between the two classes: no ereditariety, no overloading of the equal operator, no cast defined.
While if I try to do it in a Visual Studio Community 2019 on Windows, it tells me that class conversion is not defined (like I expect to).
Do you think I overlooked any of the above relationship or any other way for this to happen exists?
Edit: I know that is not a good code, that it has errors, but since it sounded so strange to me that it compiled without errors/warnings, I was just wondering if there was something I didn't know about.
Strange class cast happening
There is no cast whatsoever. Cast is an explicit conversion. What you're showing is an implicit conversion.
or any other way for this to happen exists?
The program is ill-formed. Only possibility for this to compile is a language extension of the compiler that you are using. Regardless, if there is no diagnostic message, then the compiler does not conform to the standard.
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 6 years ago.
Improve this question
I am using VS2008 and has just changed from boost 1.51 to 1.61. Unfortunately boost decided to change the boost::optional<T> and make the cast from optional to bool explicit i.e. the snippet below is not valid anymore
boost::optional<TYPE> optType;
TYPE t = default;
if (optType)
t = optType;
You must explicitly cast the optType to bool to make this work.
Even more unfortunate is the fact that this still compiles (with the wrong result ofc), and I am unable to detect where it is used. Does anyone know of any (even intrusive) methods to detect those places.
I tried to mark the implicit cast as deprecated in boosts 1.51 hpp-file but for some reason the compiler does not show any warnings.
The amount of code I have to look through is several hundred thousand rows that's why I need this.
In your particular snippet, you could simply write
boost::optional<TYPE> optType;
TYPE t = default;
if (optType)
t = true;
However, did you mean to get the TYPE value?
if (optType)
t = *optType;
You can also simply to
t = optType.get_value_or(default);
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 7 years ago.
Improve this question
It looks like a very basic question that should have an explicit googlable answer, but I wasn't able to find it so far. It seems like the answer is no, but I wanted to make sure.
What I mean is comething like this:
struct A { int a,b; };
void foo(const A&); // just some function
foo(A{1,2}); // doesn't work, but it was a long shot
foo(A({1,2})); // looks like it could have worked, but it doesn't
foo(A(std::initializer_list<int>({1,2}))); // ugly and doesn't work either anyway
Update:
option 2 actually works (as well as an even a simpler foo({1,2})), but only in vs2013, not in 2012.
Update #2: come on, was it necessary to downvote it so hard? It's still useful to know that it only works starting with vs2013.
Even easier than that. Behold the power of uniform initialization:
foo({1,2});
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.