In VC++ 2013 (and 2015 RC), I find that this results in compilation errors:
namespace namespace1
{
template<typename T>
class Bar
{};
}
namespace namespace2
{
template <unsigned Bar>
struct Foo
{
static const int value = (Bar < 1) ? 1 : 2;
};
}
Errors:
error C2059: syntax error : ')'
: see reference to class template instantiation 'namespace2::Foo<Bar>' being compiled
error C2143: syntax error : missing ')' before ';'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2143: syntax error : missing ';' before ')'
error C2059: syntax error : ')'
error C2238: unexpected token(s) preceding ';'
fatal error C1201: unable to continue after syntax error in class template definition
If I swap the order of the namespaces, I don't get an error.
Why is the compiler treating Bar as a type, when it's not qualified with the namespace?
Additionally, if I change the value initialisation to:
static const int value = (Bar > 1) ? 1 : 2;
I don't get an error either.
Encountered this while compiling Google Protocol Buffers, where this struct definition:
// Compile-time equivalent of VarintSize32().
template <unsigned Value>
struct StaticVarintSize32 {
static const int value =
(Value < (1 << 7))
? 1
: (Value < (1 << 14))
? 2
: (Value < (1 << 21))
? 3
: (Value < (1 << 28))
? 4
: 5;
};
won't compile due to a template class called Value existing in a namespace in our own codebase. I have worked around it for now by ensuring that the relevant Procotol Buffers header is included first, but it seems that this is a compiler bug?
Is there any other way to fix it, when I can't really change either Protocol Buffers code or the Value class?
Confirmed as a bug by Microsoft, will be fixed in 2015 RTM.
Related
This code compiles perfect:
if ( args.Length() > 0 ) {
if ( args[0]->IsString() ) {
String::Utf8Value szQMN( args[0]->ToString() ) ;
printf( "(cc)>>>> qmn is [%s].\n", (const char*)(* szQMN) ) ;
} ;
} ;
But this one does not :
if ( args.Length() > 0 ) {
if ( args[0]->IsString() ) {
String::Utf8Value szQMN( args[0]->ToString() ) ; // <<<< (A)
} ;
} ;
printf( "(cc)>>>> qmn is [%s].\n", (const char*)(* szQMN) ) ; // <<<< (B)
Error says : "error C2065: 'szQMN' : undeclared identifier" on line (B)
This means to me that the sentence marked (A) is a definition at the same time as an assignement, right ?
And compiler decides it is "conditionally" defined as it is within two "IF's" ?
My question is : how to move the declaration out of the two "IF's" ?
In this way I also can give it a defalut value ... in case a IF fails.
If I write this line out of the two "IF's"
String::Utf8Value szQMN ("") ;
... then I get the error :
cannot convert argument 1 from 'const char [1]' to 'v8::Handle<v8::Value>'
Any ideas?
This means to me that the sentence marked (A) is a definition at the same time as an assignement, right?
Technically it is a constructor call that creates a variable and initializes it.
Also note that automatic variables exist only until the end of the scope (usually a block inside {} brackets). That is why your second code example does not compile.
if (condition)
{
int x = 5;
}
x = 6; // error, x does not exist anymore
My question is : how to move the declaration out of the two "IF's"?
String::Utf8Value szQMN ("");
This is a constructor call of the class String::Utf8Value class. From the error message it takes a parameter of type v8::Handle<v8::Value>. Without knowing what this is I cannot give you an answer how to call it. You wanted to pass "" which is of type const char* or const char[1] and the compiler is telling you that it does not take that parameter.
Edit:
From the link that DeepBlackDwarf provided in the comment, this is how you create a Utf8Value from a string:
std::string something("hello world");
Handle<Value> something_else = String::New( something.c_str() );
So in your case you would do:
String::Utf8Value szQMN (String::New(""));
the definition in the IF's loop is only works in this loop.
So in the (B) sentence,the definition has already been expired.
If you need to use the var both in the IF's loop and outside,you can declare a global variability by this sentence :
extern String::Utf8Value szQMN( args[0]->ToString() ) ;
I do not know why I get expected a ';' error. Here is my code; I even made vector pThreadCodes static, still did not work out. Any idea pls?
vector<PrintStuff*> pThreadCodes;
for (int i = 0; i < (unsigned)strlen(str);i++){
PrintChar pstuff_char(str[i], random_delay_params::min_to_wait, random_delay_params::max_to_wait);
pThreadCodes{ &pstuff_char };// here it gives expected a ';'
}
As the title says,does the visual studio distinguish these two files by their suffix?.c or .cpp?
I also have another question.At first,I stated the program like this:
int main(int argc, char **argv)
{
LARGE_INTEGER TimeStart;
LARGE_INTEGER TimeEnd;
QueryPerformanceCounter(&TimeStart);
static double Freq;
static int getfreq;
double mu,om;
double *v;
int it,i,j;
....
}
but it brings out many problems:
1>sor2d.c(23): error C2143: syntax error : missing ';' before 'type'
1>sor2d.c(24): error C2143: syntax error : missing ';' before 'type'
1>sor2d.c(25): error C2143: syntax error : missing ';' before 'type'
1>sor2d.c(26): error C2143: syntax error : missing ';' before 'type'
23 ling points to "static double Freq;"
but if I put "QueryPerformanceCounter(&TimeStart);" after the data allocation,the compiler can succeed.Could someone tell me why this happened,was is just because of my carelessness of omitting something or ignorance...?
In C, all variables must be declared before calling any methods.
Visual Studio will, by default, compile .C files as C. You can override this.
In C89, you must declare all of your variables at the top of the code block. You may also initialize them to compile-time constants (literals, macros that expand to literals, the values of variables that have already been initialized, and any operations on the above that can be performed at compile time). You cannot intersperse other types of statements (like function calls) within these declarations.
This limitation was removed in C99 (which is not supported by Visual C++) and C++.
Language: Visual C++, MFC
I'm attempting to make an array of pointers to CString variables that I have declared in my header file. In general, this is what I'm doing:
CString *variableArray[5] = {
&var1
, &var2
, &var3
, &var4
, &var5
};
For whatever reason, though, I keep getting the following error:
Error 1 error C2065: 'var1' : undeclared identifier 18
Error 2 error C2065: 'var2' : undeclared identifier 19
Error 3 error C2065: 'var3' : undeclared identifier 20
Error 4 error C2065: 'var4' : undeclared identifier 21
Error 5 error C2065: 'var5' : undeclared identifier 22
I'm not quite sure I'm getting this error. To me knowledge, this is the correct way to make an array of pointers. Any help would be awesome!
EDIT: Here are the declarations in the header file:
public:
CString var1;
CString var2;
CString var3;
CString var4;
CString var5;
Where do you create "variableArray"? If it's in a static method or outside the scope of the class, it would make sense why you're getting that error.
I am implementing a smart pointer class using generics and I wanted to force users of this class to properly construct the smart pointer using syntax such as
MyReference<TestCls>(mytest3))
or
MyReference<TestCls> mytest4(new TestCls());
so I have used the explicit keyword on the CTOR, to prevent this:
MyReference aRef = NULL;
However due to unfortunate circumstances beyond my control, I am working on code that is compiled using the ancient MSVC++ 4.1 compiler. I get the following errors when I include the explicit keyword:
MyReference.h(49) : error C2501: 'explicit' : missing decl-specifiers
MyReference.h(51) : error C2143: syntax error : missing ';' before ''
MyReference.h(52) : error C2238: unexpected token(s) preceding ':'
MyReference.h(52) : error C2059: syntax error : 'int constant'
When I add a #define explicit those errors disappear.
This was a hack on my part, just to get the compiler to ignore the keyword. I'm guessing that this means that explicit is not supported by yon olde compiler.
Can someone confirm this and is there anyone out there with knowledge of a workaround solution for this?
Merci Beaucoups,
Dennis.
This site has a workaround for this, namely:
Unfortunately, older compilers may not
support the use of "explicit", which
could be a headache. If you're stuck
working with an out-of-date compiler
and can't get one that has better
support for the C++ standard, your
best solution may be to take advantage
of the fact that only a single
implicit conversion will take place
for a given value. You can exploit
this by using an intermediate class
that implicitly creates an object of
each type, and then have your main
class implicitly create objects from
that class:
class proxy
{
public:
proxy(int x) : x(x) {} ;
getValue() { return x; }
private:
int x;
};
class String
{
// this will be equivalent of explicit
String(proxy x) { /* create a string using x.getValue(); */ }
}