gcc TemplateClass<::GlobalSymbol> error on <:: - c++

For some reason gcc does not like when template parameter is a global namespace symbol, i.e.
TemplateClass<::GlobalSymbol>
It works when I do
TemplateClass< ::GlobalSymbol>
That is, gcc does not like to see <::
Is it possible to prevent without modifying sources (which are autogenerated)?
UPD: I don't want to modify sources. I found that -fpermissive seems to change this to a warning instead of error, but haven't found yet how to enable it from code (using pragmas for example).
UPD: Well, I found that
#pragma GCC diagnostic ignored "-fpermissive"
does the trick, anyway I accept the answer that helped me to find this out.

<: is a digraph which is equivalent to [, thus the error. Since you don't want to modify the code a workaround is to use -fpermissive command-line argument or pragma to G++ (it actually gives you a hint):
test.cpp:9:16: note: (if you use ‘-fpermissive’ G++ will accept your code)

With gcc 4.6 I get the following error:
graphs.cpp: In function ‘int main()’:
graphs.cpp:9:15: error: ‘<::’ cannot begin a template-argument list [-fpermissive]
graphs.cpp:9:15: note: ‘<:’ is an alternate spelling for ‘[’. Insert whitespace between ‘<’ and ‘::’
graphs.cpp:9:15: note: (if you use ‘-fpermissive’ G++ will accept your code)
So, -fpermissive is the way to go. [EDIT: I see now that you added that you already found this]
Else, since you mentioned that the sources are auto-generated, you could add a post processing step. For example using sed
sed -i 's,<::,< ::,g' filename.cpp
or something similar in e.g. python. But only if you are sure you never use <:: for something else.

Related

C++ error: expected primary-expression before ‘[’ token

Now I'm trying to install RealPlexor by dklab, but it's falls with errors:
# bash ./Make.sh
In file included from dklab_realplexor.cpp:68:
utils/misc.h: In function ‘void die(std::string)’:
utils/misc.h:105: error: expected primary-expression before ‘[’ token
compilation terminated due to -Wfatal-errors.
Here is that line
s = regex_replace(s, regex("\\$!"), [](smatch s) { return strerrno(); });
Make sure that you are passing the following flag to your compiler (as described in the the g++ documentation):
-std=c++11
This tells the gcc compiler (g++) to compile your code with C++11 semantics.
The lambda expression syntax you are using (the part starting with []) is a C++11 feature, and will cause compilers great confusion if it appears in code that they aren't expecting to be C++11.
However, as has been pointed out in another comment here (and is confirmed by this table, the version of gcc you are running (4.4.5, per a comment) doesn't have lambda expression support. May have to use a function object instead, or upgrade to a newer version of gcc/g++.
Just say
s = regex_replace(s, regex("\\$!"), *(smatch s) { return strerrno(); });
The [] operator is usually used to index something (like a character array), so C++ expects something in front of it
Also try this suggestion from #DavidO:
You're using a lambda expression which is a C++11 syntax, but probably haven't set your compiler to recognize C++11. If you're using g++, you would use the -std=c++11 flag.

C++ : Unit testing using Google Mock with Code::Blocks, MinGW and C++11

I've been trying to learn unit testing on my own following along a book.
Code in the book use the C++11 standard and have a line like this:
auto variable = function(parameter);
When I first compiled it I got this warning:
warning: 'auto' changes meaning in C++11; please remove it [-Wc++0x-compat]
No biggie, I could fix that by checking the following box in the Project->Build options... menu:
[ ] Have g++ follow the C++ 11 ISO C++ language standard [-std=c++11]
Now, however, I get new errors related to Google Mock in the gtest-port.h :
| | In function 'int testing::internal::posix::StrCaseCmp(const char*, const char*)':
|1719| error: '_stricmp' was not declared in this scope
| | In function 'char* testing::internal::posix::StrDup(const char*)':
|1721| error: '_strdup' was not declared in this scope
| | In function 'FILE* testing::internal::posix::FDOpen(int, const char*)':|
|1779| error: 'fdopen' was not declared in this scope
Searching for this problem yielded little for me but I did try and define the target OS as it was a suggested solution in case it was not correctly identified automatically. Adding GTEST_OS_WINDOWS=1 and/or GTEST_OS_WINDOWS_DESKTOP=1 in the projects defines changed nothing.
I realize this is easily fixed in this instance by just providing the correct type instead of using auto but I'd like to find a solution for this if possible. Replacing auto and not having the -std=c++11 option checked makes the code work as intended so the library works.
I'm using Code::Blocks 13.12 , MinGW/g++ 4.8.1-4 and Google Mock 1.7 in Windows.
Thanks for reading =)
The answer here lies in the functions which are missing declarations: _stricmp, _strdup and fdopen. The first two are Microsoft versions of the POSIX functions stricmp and strdup. Note that you are specifying the use of the C++11 ISO standard which does not contain items in the POSIX standard. By specifying --std=gnu++11 you are telling the compiler to accept a hybrid of C++11 and POSIX along with GNU extensions.
Interestingly I cannot replicate this with GCC 4.8.2 on Linux so there is the possibility that something else is going on in the Google Mock headers when compiling on Windows.
Answering for anyone still facing the same issue:
The source of this issue might be in your CMakeLists file.
Set your compiler flags to -std=gnu++ instead of -std=c++
One way to do it would be to include
set(CMAKE_CXX_FLAGS "-std=gnu++0x")
to your CMakeLists file.

How do you make the warning come up that has the words 'suspicious' and '-Wmain' in it?

It's a simple question really, and refers to Linux (as opposed to Windows or Mac).
How do you generate a warning message from the C or C++ compiler that must have the word 'suspicious' in it, and must refer to (-Wmain).
(update)
Thanks Boann - I got some of those Warnings, but I also got Error - error: ‘::main’ must return ‘int’.
The reason I'm asking this question is that a week ago my compiler (GCC 4.8.1) came out with this warning saying 'suspicious' and that it was caused by Wmain. So I put -Wno-main and the warning went away and it compiled fine. Just recently it has started complaining making it an actual Error and not compiling. So I'm kind of worried that somehow the mother ship has covertly updated my compiler over the internet, without me knowing, and changed it to treat it as error. (I was probably using 'int4' as the return type which I forever have typedef'd as signed long int.
I note there's -Wmain referenced in g++ man page so it must be for something, but what warning is there that isn't overruled by an error??
At offset 557284 (decimal) of the g++ executable I found "Warn about suspicious declarations of "main".
For what it's worth,
struct suspicious {};
int main(suspicious) {}
Output with g++ -Wall, GCC 4.8.2:
warning: first argument of 'int main(suspicious)' should be 'int' [-Wmain]
If you refer to the -Wmain parameter of GCC, you get the warning by giving main odd arguments, an odd return type, or giving it static linkage. This will do it:
static float main(float x) {
return 0;
}
Compiled with gcc -Wmain, it displays these warnings; they do not actually contain the word 'suspicious' though:
warning: return type of 'main' is not 'int'
warning: first argument of 'main' should be 'int'
warning: 'main' takes only zero or two arguments
warning: 'main' is normally a non-static function
I just downloaded the sources for gcc 4.8.1 and searched all relevant files for the word "suspicious".
There are a number of occurrences, but as far as I can tell, there's no way an error message for a C or C++ source file can contain the word "suspicious". It's possible but unlikely that there was a local modification.
Is it possible that you're mistaken about what the error message said? If you have a log containing the error message, please update your question to show the exact message your received.
If you're concerned that your compiler may have been updated without your knowledge, you might check the timestamp of the compiler executable and of any programs it invokes (use gcc -v to check that). But gcc itself doesn't automatically update itself. If you're using it on a system administered by someone else, automatic updates are to be expected. If you administer the system yourself, you may have configured it to update software without manual intervention; if so, that's not a gcc issue. I don't know what "mother ship" you're referring to.

How to fix "error: no previous declaration for void awn::vala_array_destroy"?

I'm on Ubuntu 13.04 32 platform. I'm trying to compile awn with latest vala. I get following error message.
vala-utils.cc: In function 'void awn::vala_array_destroy(gpointer, gint, GDestroyNotify)':
vala-utils.cc:358:6: error: no previous declaration for 'void awn::vala_array_destroy(gpointer, gint, GDestroyNotify)' [-Werror=missing-declarations]
cc1plus: some warnings being treated as errors
How can I fix that error?
At the top of vala-utils.cc insert the function prototype:
void vala_array_destroy(gpointer array, gint array_length, GDestroyNotify destroy_func);
This will silence the warning which is causing the build to fail due to -Werror defined in the Makefile. Consider raising a pull request for the benefit of the maintainers.
Alternatively modify ln.89 in configure.ac and remove the -Werror to stop the warning failing the build.
This actually a warning, and a rather pedantic one at that.
It's saying that the global function awn::vala_array_destroy has not been declared beforehand. Usually this is a sign of a programmer's mistake, but from the source it seems to me that:
this function is only supposed to be used by awn::vala_array_free directly below it (in which case it should really have been given internal linkage), or
a declaration is available in some header elsewhere (in which case it's probably in the wrong header, as it ought to be in vala-utils.h).
The long and short of it is that the code is probably fine, if a little strangely designed. This case is being treated as an error because you have -Werror turned on for this warning, turning it into an error.
I don't know how you're building the library, but you could adjust the compiler flags to avoid this; by default, GCC does not emit this warning, nor turn it into an error. Alternatively, you could add the function declaration into vala-utils.h yourself. You should also take up the issue with the library's maintainers.
Update: I found the culprit on line 89 of AWN's configure.ac. You should definitely take this up with the library maintainers. I'd be amazed if this had not already been reported as a bug; I believe it was introduced in this revision.

compiling zthreads

I downloaded zthreads (found here: http://zthread.sourceforge.net/) and tried to compile but I get this error from make:
MutexImpl.h:156: error: there are no arguments to 'ownerAcquired' that depend on a template parameter, so a declaration of 'ownerAcquired' must be available
MutexImpl.h:156: error: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
and then after that for every function in that source file I get this kind of error:
MutexImpl.h:167: error: there are no arguments to 'function' that depend on a template parameter, so a declaration of 'function' must be available
So I'm guessing it's a makefile error but I'm not for sure how to tell make to tell g++ to compile the files with -fpermissive. Does anyone know how to put that into the makefile (if that is the problem)?
CXXFLAGS += -fpermissive
Standard gmake convention is to use the CXXFLAGS variable to pass options to the C++ compiler. You can take advantage of that fact as well as a feature called "command-line overrides" to get your extra flag tacked onto the flags passed to g++ by invoking gmake this way:
make CXXFLAGS+=-fpermissive
I downloaded the source myself to verify that this works and found that it does, although there are still a bunch of other warnings emitted. You may wish to log a bug for these issues if you intend to continue using the library.
Hope this helps,
Eric Melski
I got rid of all these errors changing the code: for each line with that mistake, add this-> to the function that provokes the error. In the line you quote:
ownerAcquired must be changed by this->ownerAcquired
I hope this helps