I used CLion as an IDE, it reports an error in IDE as
field z must be initialized
It can compile and run. But if I change const int z{3}; to const int z=3;, no error will be reported in IDE. My question is whether it is indeed an error of my codes or it is just a bug in the IDE? Any difference between these two initialization approaches? Did your IDE report this error?
#include <iostream>
using namespace std;
class Test
{
private:
const int x = 3;
int y;
const int z{3};
public:
Test(int);
int gety(){
return y;
}
};
Test::Test(int a){
y=x+4;
}
int main()
{
Test test(5);
std::cout << test.gety() << std::endl;
return 0;
}
whether it is indeed an error of my codes
There is no error in the code, it is OK.
or it is just a bug in the IDE?
It is a bug in whatever generates the error message. The IDE is high on my list of suspects but it could be another tool whose message the IDE relays.
Any difference between these two initialization approaches?
In this context (default member initializer) both syntaxes are semantically equivalent. There is no difference.
Related
Note: There are a number of questions on Stack Overflow with very similar-looking titles, but none that I have found is actually a duplicate, IMHO.
I have been using code like the following in my project(s) for a number of years, without problem. However, since a recent update to Visual Studio 2019 (16.7.2 - though it may have been at 16.7.1), the MSVC compiler has started to generate the error shown (I have the compilation 'Standard' set to C++17).
#include <iostream>
class Foo {
public:
Foo() { }
static constexpr char Letters[6][10] = { "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot" };
};
int main()
{
Foo f;
for (int i = 0; i < 6; ++i) std::cout << f.Letters[i] << std::endl;
return 0;
}
Error (at the opening brace in the constexpr line):
error C2131: expression did not evaluate to a constant message :
failure was caused by a read of an uninitialized symbol
The clang-cl compiler continues to accept the code without any warning.
I have a fairly 'trivial' fix for this issue, as below:
class Foo {
public:
Foo() { }
inline static const char Letters[6][10] = { "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot" };
};
However, I am intrigued by the error report. Is there something I am missing (and have been missing for ~5 years), or is this a bug in the latest release of MSVC? If the former, what is my error or invalid assumption?
Edit with respect to solution suggestion:
The solution for the author of the question was that he put the main function into a namespace. Then he added parameters to the main function, then he used extern "C" in order to get his main to be recognized as entry point.
Answers, which looked detailed suggested that the code author did not define a main/win main function (which was debated). I also don't use Visual C++, but a mingw version which works on my AMD computer (its a win 64 variant on a windows 10 home edition).
I have a problem, which seems frequently to find its way to StackOverflow and I could not locate any answer which helped me resolve my problem.
I used codeblocks and made a project as a console application.
This is the code I tried to compile and I keep on getting the "undefined WinMain" error.
I used the win64 MinGW compiler, which came with codeblocks
Compiler Flags were: (-std=C++11, -std=C++14, and -std=C++17. I used all of previous one at a time, before), -Wall, and -pedantic
This is the code and naturally, I was not able to resolve my problem. The attached images are "Build log" and "Build Messages" from the respective codeblock tabs of the "Logs & other window".
Please help.
#include <iostream>
using namespace std;
class Rectangle {
protected:
int width, height;
public:
Rectangle(int width = 0, int height = 0): width(width), height(height) {};
int get_width() const {return width;}
int get_height() const {return height;}
virtual void set_width(int width) {this->width = width;}
virtual void set_height(int height) {this->height = height;}
int area() const {return width * height;}
};
class Square : public Rectangle {
public:
Square(int size = 0) : Rectangle(size, size) {
set_width(size);
this->width = this->height = size;
}
void process(Rectangle &r){
r.set_height(10);
cout << "expected area was 30, got " << r.area() << std::endl;
}
int main() {
Rectangle r(3,4);
process(r);
std::cout << "Template" << std::endl;
return 0;
}
};
Possibly this is part of the solution:
I know that extra compiler flags can be created.
This is a warning, which showed, after I wrapped my main function with extern "C" as suggested by one of the answers in your solution suggestion
||warning: command line option '-std=c17' is valid for C/ObjC but not for C++|.
In my case I chose the option, where it said -std=c++17, still this warning came up.
Well, I wrote a simple code to check the possibility of creating objects using 'new' operator. When I was trying to compile the code, the MS Visual Studio threw the error like this: " Error: Unable to open file C:\Users...\test1\Debug\main.obj. Error code = 0x80070002.Error: Could not find 'C:\Users...\test1\Debug\main.obj'. test1.exe was built with /DEBUG:FASTLINK which requires object files for debugging.
What is going on? Please help.
Code:
#include <iostream>
class czlowiek {
int wiek;
char plec;
czlowiek();
czlowiek(int Wiek, int Plec);
};
czlowiek::czlowiek(int Wiek, int Plec) {
wiek = Wiek;
plec = Plec;
}
int main()
{
czlowiek *first;
first = new czlowiek();
delete first;
std::cin.get();
return 0;
}
The code you posted will not link:
The constructor czlowiek() doesn't have an implementation.
Both constructors are private (in classes members and methods are private by default).
As warning, you are assigning a int to a char (plec).
Consider following code:
#include <iostream>
inline namespace N1
{
int x = 2;
}
int x = 1;
int main()
{
std::cout << N1::x;
std::cout << x;
return 0;
}
This obivously gives me error on std::cout << x;
reference to x is ambiguous.
::x also does not work.
I understand why it happens, but how can I solve this problem without renaming or removing variables or namespaces? Or it is the only solution?
Inline namespace scoped variables have static storage duration (internal linking). So declaring
extern int x;
just before displaying x will do it for you
Live on Coliru.
This way, the N1::x won't be considered during name lookup, as it has static storage duration and internal linking.
It's not entirely clear why the code works, so I follow up with a question here.
Why does the following code compile with a circular const variable referencing itself?
#include <iostream>
extern int main(int argc, char* argv[])
{
const int foo = foo * 60;
std::cout << foo << std::endl;
return 0;
}
I'm compiling on a Solaris 5.10 x86 host with the SUNWspro compiler:
/opt/SUNWspro/bin/CC test.cpp
For completeness, this is what it prints:
$ ./a.out
-519270512
In C++, variables are in scope and can be used as part of their own initializers. For example, the following code is also legal:
int x = x;
Doing this results in undefined behavior, since you're referring to the value of x before it has been initialized. However, it's perfectly legal code. I think that the rationale behind this is that you might in some cases want an object to refer to itself during its construction. For example, you could conceivably do something like this:
MyObject x(137, &x); // Pass a pointer to x into its own constructor
I'm honestly not sure why you'd ever want to do this, though.
Hope this helps!