Here is the sample code that I ran on Visual Studio 2010:
#include <iostream>
int main()
{
int **p(NULL);
}
I get this error: error C2059: syntax error : 'constant'
But if I change int **p(NULL); to int **p = NULL; the above code compiles fine.
Checked this on GCC(Version:4.4.2) and both work fine. What am I missing here?
VC++ compiler seems confused about initializations of pointer to pointer ...
This works for example
int (**p)(NULL);
These don't
int *i;
int **p(&i);
int **o(NULL);
This works though
int (**p)(&i);
typedef int* intp;
intp *o(NULL);
etc... the pattern is initialization fails whenever two ** are present! I'd guess a bug! Someone from MSVC team might be able to confirm
That is either a bug in the compiler itself, or possibly you've done something and asked something else.
MSVC10 support few features from C++11, such as the following:
int **p1 = nullptr;
int **p2{}; //initialized to nullptr!
You can try any of these. Both are fine.
Looks like, defect with Visual studio, It works if i use c++ to compile # http://codepad.org/ and run the following code
int main()
{
int **p(NULL);
return 0;
}
Same works using g++ compiler as well.
You get a syntax error: apparently NULL is not defined. You should include cstdlib.
Related
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).
This question already has answers here:
Why does stdafx.h work the way it does?
(4 answers)
Closed 5 years ago.
I'm facing a really stupid and infuriating problem, I was following a video on how to make a roguelike, I decided to stop after knowing how to print a level to the screen and actually start coding, then I got multiple errors on seamingly perfect code, I blammed the constructors so I decided to make a new project on visual studio to test the most stupid case, one main fuction, one clase with one constructor that does nothing, and I leaved all the work of making the class and the constructor to visual studio, I still got the goddammned issue, here's the error on that simplified version of the code, I would show the rogue like code but I would have to translate to english al the errors myself, and most of them have nothing to do with the actual error, because mostly they're complaining about missing ; in places where either there shouldn't be a ; or there alredy is one.
here's the code
main.cpp:
#include "stdafx.h"
int main()
{
return 0;
}
test.h
#include "stdafx.h"
class test
{
public:
test();
};
test.cpp
#include "test.h"
#include "stdafx.h"
test::test(){
}
yet with that simple auto-generated code, visual studio still complaints and gives me this error messages
c4430 missing type specifier - int assumed. Note: C++ does not support default-int
c2653 'test' : is not a class or namespace name
'test' : function should return a value; 'void' return type assumed
what should I do?
You have to define return type explicitly and return a value of that type if your function returns not void. Otherwise you will get that error.
class A
{
int int_func();
void void_func();
};
int A::int_func()
{
return 0;
}
void A::void_func()
{
// No return;
}
My code looks like this:
#include <stdio.h>
#pragma pack(1)
class MyClass
{
bool a;
bool b;
int c;
char d[3];
bool e[4];
};
#pragma pack()
int main()
{
printf("sizeof(MyClass)=%d\n", sizeof(MyClass));
return 0;
}
The output is:
sizeof(MyClass)=13
But when I "hover" over sizeof(MyClass) I get:
This wouldn't have been a big issue but I'm trying to implement a compile-time assertion and it isn't working (getting a red underline):
Anyone have any idea how to fix this?
This is not a difference between compile-time and run-time; it is a difference between your compiler and your IDE's "intellisense", the latter of which appears not to support/recognise the #pragma pack directive.
Ignore it. The size is 13.
Since you can actually build your program, you know that the compile-time assertion succeeds, despite the "red line".
It is probably worth adding a comment before that assertion, explaining that users of Visual Studio 2015 will see a false negative in their IDE for the following assertion.
You may also wish to raise a bug on Microsoft Connect, if there is not one already.
For some reason the code completion in netbeans can't figure out the return type of templated functions. Take the following example...
struct Test
{
int val;
};
int main()
{
vector<Test> v;
Test t = {10};
v.push_back(t);
cout << v[0].val; //Netbeans gives the warning "Unable to resolve identifier val"
return 0;
}
The code compiles and runs fine but what is annoying is that I get this error all over my code when I use vectors. Also the code completion does not seem to work. When I type v[0]. there is no drop down giving me to option to choose val.
I am using netbeans 7.4 along with 64bit MinGW.
Well there seems to bug in the Netbeans 7.2 version and later it was fixed.
https://netbeans.org/bugzilla/show_bug.cgi?id=172227
You can find the complete discussion and possible resolution on the same problem from the following link. Here you can find how to resolve this problem(possibly).
Netbeans 7.2 shows "Unable to resolve identifier" , although build is successful
Follow some simple steps to get your identifiers resolved, given on the following link
Netbeans 7.2 shows "Unable to resolve identifier" , although build is successful1
try changing
struct Test
{
int val;
};
with
typedef struct
{
int val;
} Test;
in pure C "Test" would not be a defined type but "struct Test" would be. By changing to a typedef then you then have "Test" as a defined type.
The following code doesn't compile for obvious reasons, namely that Foo is trying to access a private member of Bar. However if you uncomment/comment the lines marked, making Foo a template, it does compile and outputs 42. What am I missing here? Why does this work? Seems to me it shouldn't.
Thanks for your help.
#include <iostream>
class Bar {
private:
static const int x = 42;
};
//template <int> // uncomment me
struct Foo {
static const int i = Bar::x;
};
int main(int argc, char* argv[]) {
std::cout << Foo::i << std::endl; // comment me
//std::cout << Foo<0>::i << std::endl; // uncomment me
}
If you are seeing this behavior, it is a compiler bug.
Both Comeau Online and Visual C++ 2010 reject the code as invalid because Bar::x is inaccessible. g++ 4.1.2 incorrectly accepts the invalid code (someone would need to test with a later version to see if it's been fixed; that's the only version I have on this laptop).
This seems like GCC bug 40843. It is listed as UNCONFIRMED, but I can reproduce it on g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3 as well.
VisualStudio 2010 said "error C2248: 'Bar::x' [...]
As the plateform was not speciifed, I have assessed that the assumption is false almost on Windows VC9.