Having such a super simple char array definition:
const int g_msgbuf_size = 200;
char g_msgbuf[g_msgbuf_size];
giving me an totally unreasonable error:
error C2057: expected constant expression
on the second line while compiling in MSVC 2019.
When i try the code on any other c++ compiler like https://www.onlinegdb.com/online_c++_compiler or http://cpp.sh/ everything goes well - I get no errors as expected.
Why does the MSVC 2019 makes me problems?
Related
I am trying to compile some old C++ in Visual Studio 2008 that compiles in Visual C++ 6. I am trying to deal with the errors through configuration changes and compiler instructions instead of changing the code.
In a lot of places in the code, there are variables and functions declared without a type. The compiler gives an error like "error C4430: missing type specifier - int assumed. Note: C++ does not support default-int".
For example:
class DLL WebPage
{
private:
char *ip_;
char *port_;
char *page_;
public:
WebPage();
~WebPage();
get(char *ip,char *port,char *page); //<---This is the trouble maker
char *ip(void);
char *port(void);
char *page(void);
};
Any way to get this to compile in a newer IDE?
Here is my enum declaration :
enum connection_primary_identifier_e : uint64_t
{
INVALID_IDENTIFIER = std::numeric_limits<std::underlying_type<connection_primary_identifier_e>::type>::max(),
}
(same happens if I use uint64_t directly as the type, also if I use -1 or -1ULL)
When I try to compile the file I get the following errors / warnings :
error: integer constant is so large that it is unsigned [-Werror]
error: narrowing conversion of ‘18446744073709551615I128’ from ‘__int128’ to ‘unsigned int’ inside { } [-Werror=narrowing]
error: large integer implicitly truncated to unsigned type [-Werror=overflow]
cc1plus: all warnings being treated as errors
The really weird thing is, the errors are actually produced for non-existing lines (line number is 3 after the last line on file) on another file (which uses the enum), I made sure it isn't a missing parentheses or anything like that.
Update:
Using uint32_t doesn't produce the error.
Using g++ (GCC) 4.8.3
Might be because std::underlying_type was initially underspecified and didn't require a complete type. This unintentionally allowed precisely this code, which uses connection_primary_identifier_e while it's still incomplete.
Starting with C++17, your code is definitely illegal.
I encountered a compiler crash and intellisense false positives with Visual Studio 2015 using C++.
This crashes the compiler when written within a function block:
if();
This is the dialog that is shown when compiling (I am on a German version of Windows):
Even though the compiler crashes, I get error list output:
Error C2059 syntax error: ')'
Warning C4390 ';': empty controlled
statement found; is this the intent?
Error C1903 unable to recover from previous error(s); stopping compilation
This produces squiggles and error annotations in the vertical scrollbar in map mode, but no actual intellisense errors:
#include <vector>
struct S { std::vector<S> Children; };
int main(int argc, char* argv[]) {
S item;
item.Children.push_back(S());
// ^
// Error: no instance of overloaded function
// "std::vector<_Ty, _Alloc>::push_back [with _Ty=S, _Alloc=std::allocator<S>]"
// matches the argument list
// argument types are: (S)
// object type is: std::vector<S, std::allocator<S>>
S& back = item.Children.back();
// ^^^^
// Error: a reference of type "S &" (not const-qualified) cannot be
// initialized with a value of type "S"
return 0;
}
Are those bugs? Are they known? Can you reproduce them?
For the first case: the compiler shouldn't crash but just issue the diagnostic you show. So yes, that's a bug. Which doesn't occur in VS2013 btw. Submit a report for it here
For the second case: it is the same in VS2013 and is due to nesting a vector of S inside S. This and other cases make the error squiggles appear incorrectly, it is actually not that uncommon. But ideally it should not happen so you can submit a bug report for it as well, though it might be something which is going to be labelled 'wontfix' as the compiler team usually focusses on more urgent cases.
I am using Orwell Dev C++ IDE. Recently I tested following simple program in which I forgot to put semicolon (;) but still it compiles fine in C but not in C++. Why? What is the reason?
// C program compiles & runs fine, even ; missing at end of struct
#include <stdio.h>
struct test
{ int a,b}; // missing semicolon
int main()
{
struct test d={3,6};
printf("%d",d.a);
return 0;
}
[Warning] no semicolon at end of struct or union [enabled by default]
// Following is compilation error in C++
#include <stdio.h>
struct test
{ int a,b}; // missing semicolon
int main()
{
struct test d={3,6};
printf("%d",d.a);
return 0;
}
[Error] expected ';' at end of member declaration
I also tried same C program in codeblocks 13.12 IDE but it shows following error message
error: no semicolon at end of struct or union.
Why different error messages given by different implementations?
The semicolon is required by both languages. Specifically, C specifies the declaration of one or more structure members as
struct-declaration:
specifier-qualifier-list struct-declarator-list ;
and C++ specifies the declaration of one or more class member variables as
member-declaration:
attribute-specifier-seq<opt> decl-specifier-seq<opt> member-declarator-list<opt> ;
both of which require a semicolon at the end.
You'll have to ask the compiler writers why their C++ compiler is more strict than their C compiler. Note that the language specifications only require a "diagnostic" if a program is ill-formed, so it's legitimate either to issue a warning and continue compiling as if the semicolon were present, or to issue an error and stop.
It looks like your IDE is using GCC as its compiler; in which case you could use -Werror to convert warnings into errors, if you'd prefer stricter diagnostics.
I am experiencing some troubles compiling a c++ file that worked well as a previous build under GCC.
The issue is, I am using vectors of variable array size:
unsigned int howmany;
std::vector<int>* array_adresses[howmany];
I am currently using the Visual-Studio 2010 C++ compiler to built Matlab 64-bit Mex-Files.
Since VC++ won't allow me to use arrays whose size is unknown at compile time, I am receiving the following error messages:
error 2057: constant expression expected
error 2466:
error 2133: unknown size
Is there any way to build the 64 bit mex file using a GCC-compiler option or build it with a different 64-bit compiler under Matlab?
Thanks in advance!!
howmany needs to be constant, and needs to be a defined amount, like so:
const unsigned int howmany = 5;
std::vector<int>* array_adresses[howmany];
Or you can define it dynamically like this:
unsigned int howmany = 5;
std::vector<int>* array_adresses = new std::vector<int>[howmany];
C++ standard doesn't allow variable-length arrays. Lets take this code:
int main(int argc, char *argv[])
{
int a[argc];
return 0;
}
This compiles fine with g++ foo.cpp, but fails if you require a strict standard compliance.
g++ foo.cpp -std=c++98 -pedantic:
foo.cpp: In function ‘int main(int, char**)’:
foo.cpp:8: warning: ISO C++ forbids variable length array ‘a’
You should use vector<vector<int> *> or vector<int> ** instead as others already suggested.
Simply replace
int ptr[howmany];
with
vector<int> ptr(howmany);
to obtain also automatic deallocation at the end of the scope.