C++ Missing Initializer error - c++

I am getting this error during compile time (g++ 4.4.6):
main.cpp: In function ‘int main()’:
main.cpp:27: error: expected initializer before ‘:’ token
main.cpp:33: error: expected primary-expression before ‘for’
main.cpp:33: error: expected ‘;’ before ‘for’
main.cpp:33: error: expected primary-expression before ‘for’
main.cpp:33: error: expected ‘)’ before ‘for’
main.cpp:33: error: expected initializer before ‘:’ token
main.cpp:36: error: could not convert ‘((list != 0u) ? (list->SortedList::~SortedList(), operator delete(((void*)list))) : 0)’ to ‘bool’
main.cpp:37: error: expected primary-expression before ‘return’
main.cpp:37: error: expected ‘)’ before ‘return’
My code is as follows:
#include <iostream>
#include "Student.h"
#include "SortedList.h"
using namespace std;
int main() {
SortedList *list = new SortedList();
Student create[100];
int num = 100000;
for (Student &x : create) { // <--Line 27
x = new Student(num);
num += 10;
}
for (Student &x : create)
list->insert(&x);
delete list;
return 0;
}
Anybody who possibly knows the source of the error would be of great help. Also, Student and SortedList are objects which are declared in their .h files.

According to this page on GCC's website, range-based for is only available in g++ 4.6 and up, so you'll have to convert your code to a normal for loop or use std::for_each or something, or upgrade your compiler.

Related

Why does the following code work on online ide(gcc 7.2.0) but gives error on ubuntu?

When I run the following code on ubuntu(gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4)):
#include<iostream>
#include<vector>
#include<list>
using namespace std;
int main(){
vector <int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
list<int> temp;
for(auto i:v){
cout<<i<<" ";
temp.push_back(i);
}
for(auto i:temp){
cout<<i<<" ";
}
}
I get the following errors:
try.cpp: In function ‘int main()’:
try.cpp:13:10: error: ‘i’ does not name a type
for(auto i:v){
^
try.cpp:17:1: error: expected ‘;’ before ‘for’
for(auto i:temp){
^
try.cpp:17:1: error: expected primary-expression before ‘for’
try.cpp:17:1: error: expected ‘;’ before ‘for’
try.cpp:17:1: error: expected primary-expression before ‘for’
try.cpp:17:1: error: expected ‘)’ before ‘for’
try.cpp:17:10: error: ‘i’ does not name a type
for(auto i:temp){
^
try.cpp:20:1: error: expected ‘;’ before ‘}’ token
}
^
try.cpp:20:1: error: expected primary-expression before ‘}’ token
try.cpp:20:1: error: expected ‘;’ before ‘}’ token
try.cpp:20:1: error: expected primary-expression before ‘}’ token
try.cpp:20:1: error: expected ‘)’ before ‘}’ token
try.cpp:20:1: error: expected primary-expression before ‘}’ token
But when I run the code on online ide I works fine.
What is the problem with the code?
The link for code on online ide:No errors
Your code uses some of the C++11 features such as range based loops and auto specifier but you don't compile for the C++11 standard. You need to enable the C++11 support by including the -std=c++11 flag when compiling:
g++ -std=c++11 -o try try.cpp
The online compiler has this enabled by using the -std=gnu++1z flag.

Compiling a quickfix program

I am trying to use the quickfix library to connect to a broker using the FIX protocol. I just built the library using the documentation they provide and use their sample code right now
#include "quickfix/FileStore.h"
#include "quickfix/FileLog.h"
#include "quickfix/SocketAcceptor.h"
#include "quickfix/Session.h"
#include "quickfix/SessionSettings.h"
#include "quickfix/Application.h"
int main( int argc, char** argv )
{
try
{
if(argc < 2) return 1;
std::string fileName = argv[1];
FIX::SessionSettings settings(fileName);
MyApplication application;
FIX::FileStoreFactory storeFactory(settings);
FIX::FileLogFactory logFactory(settings);
FIX::SocketAcceptor acceptor
(application, storeFactory, settings, logFactory /*optional*/);
acceptor.start();
// while( condition == true ) { do something; }
acceptor.stop();
return 0;
}
catch(FIX::ConfigError& e)
{
std::cout << e.what();
return 1;
}
}
However, when I try to compile it with:
g++ fix.cpp -fexceptions -finline-functions -lquickfix -lpthread -lxml2
I get a bunch of errors:
In file included from /usr/local/include/quickfix/Session.h:34:0,
from fix.cpp:6:
/usr/local/include/quickfix/DataDictionaryProvider.h:54:72: error: ‘ptr::shared_ptr’ has not been declared
void addTransportDataDictionary(const BeginString& beginString, ptr::shared_ptr<DataDictionary>);
^
/usr/local/include/quickfix/DataDictionaryProvider.h:54:82: error: expected ‘,’ or ‘...’ before ‘<’ token
void addTransportDataDictionary(const BeginString& beginString, ptr::shared_ptr<DataDictionary>);
^
/usr/local/include/quickfix/DataDictionaryProvider.h:55:70: error: ‘ptr::shared_ptr’ has not been declared
void addApplicationDataDictionary(const ApplVerID& applVerID, ptr::shared_ptr<DataDictionary>);
^
/usr/local/include/quickfix/DataDictionaryProvider.h:55:80: error: expected ‘,’ or ‘...’ before ‘<’ token
void addApplicationDataDictionary(const ApplVerID& applVerID, ptr::shared_ptr<DataDictionary>);
^
/usr/local/include/quickfix/DataDictionaryProvider.h:63:25: error: ‘shared_ptr’ is not a member of ‘ptr’
std::map<std::string, ptr::shared_ptr<DataDictionary> > m_transportDictionaries;
^
/usr/local/include/quickfix/DataDictionaryProvider.h:63:25: error: ‘shared_ptr’ is not a member of ‘ptr’
/usr/local/include/quickfix/DataDictionaryProvider.h:63:55: error: template argument 2 is invalid
std::map<std::string, ptr::shared_ptr<DataDictionary> > m_transportDictionaries;
^
/usr/local/include/quickfix/DataDictionaryProvider.h:63:55: error: template argument 4 is invalid
/usr/local/include/quickfix/DataDictionaryProvider.h:63:57: error: expected unqualified-id before ‘>’ token
std::map<std::string, ptr::shared_ptr<DataDictionary> > m_transportDictionaries;
^
/usr/local/include/quickfix/DataDictionaryProvider.h:64:25: error: ‘shared_ptr’ is not a member of ‘ptr’
std::map<std::string, ptr::shared_ptr<DataDictionary> > m_applicationDictionaries;
^
/usr/local/include/quickfix/DataDictionaryProvider.h:64:25: error: ‘shared_ptr’ is not a member of ‘ptr’
/usr/local/include/quickfix/DataDictionaryProvider.h:64:55: error: template argument 2 is invalid
std::map<std::string, ptr::shared_ptr<DataDictionary> > m_applicationDictionaries;
^
/usr/local/include/quickfix/DataDictionaryProvider.h:64:55: error: template argument 4 is invalid
/usr/local/include/quickfix/DataDictionaryProvider.h:64:57: error: expected unqualified-id before ‘>’ token
std::map<std::string, ptr::shared_ptr<DataDictionary> > m_applicationDictionaries;
^
/usr/local/include/quickfix/DataDictionaryProvider.h: In member function ‘void FIX::DataDictionaryProvider::addTransportDataDictionary(const FIX::BeginString&, const string&)’:
/usr/local/include/quickfix/DataDictionaryProvider.h:58:45: error: ‘shared_ptr’ is not a member of ‘ptr’
{ addTransportDataDictionary(beginString, ptr::shared_ptr<DataDictionary>( new DataDictionary(path) )); }
^
/usr/local/include/quickfix/DataDictionaryProvider.h:58:75: error: expected primary-expression before ‘>’ token
{ addTransportDataDictionary(beginString, ptr::shared_ptr<DataDictionary>( new DataDictionary(path) )); }
^
/usr/local/include/quickfix/DataDictionaryProvider.h: In member function ‘void FIX::DataDictionaryProvider::addApplicationDataDictionary(const FIX::ApplVerID&, const string&)’:
/usr/local/include/quickfix/DataDictionaryProvider.h:60:45: error: ‘shared_ptr’ is not a member of ‘ptr’
{ addApplicationDataDictionary(applVerID, ptr::shared_ptr<DataDictionary>( new DataDictionary(path) )); }
^
/usr/local/include/quickfix/DataDictionaryProvider.h:60:75: error: expected primary-expression before ‘>’ token
{ addApplicationDataDictionary(applVerID, ptr::shared_ptr<DataDictionary>( new DataDictionary(path) )); }
^
fix.cpp: In function ‘int main(int, char**)’:
fix.cpp:19:5: error: ‘MyApplication’ was not declared in this scope
MyApplication application;
^
fix.cpp:19:19: error: expected ‘;’ before ‘application’
MyApplication application;
^
fix.cpp:23:8: error: ‘application’ was not declared in this scope
(application, storeFactory, settings, logFactory /*optional*/);
^
root#luis:/home/luis/tradingbot/bot4# In file included from /usr/local/include/quickfix/Session.h:34:0,
In: command not found
root#luis:/home/luis/tradingbot/bot4# from fix.cpp:6:
from: can't read /var/mail/fix.cpp:6:
root#luis:/home/luis/tradingbot/bot4# /usr/local/include/quickfix/DataDictionaryProvider.h:54:72: error: ‘ptr::shared_ptr’ has not been declared
bash: /usr/local/include/quickfix/DataDictionaryProvider.h:54:72:: No such file or directory
root#luis:/home/luis/tradingbot/bot4# void addTransportDataDictionary(const BeginString& beginString, ptr::shared_ptr<DataDictionary>);
bash: syntax error near unexpected token `('
root#luis:/home/luis/tradingbot/bot4# ^
^: command not found
root#luis:/home/luis/tradingbot/bot4# /usr/local/include/quickfix/DataDictionaryProvider.h:54:82: error: expected ‘,’ or ‘...’ before ‘<’ token
bash: ’: No such file or directory
root#luis:/home/luis/tradingbot/bot4# void addTransportDataDictionary(const BeginString& beginString, ptr::shared_ptr<DataDictionary>);
bash: syntax error near unexpected token `('
root#luis:/home/luis/tradingbot/bot4# ^
^: command not found
root#luis:/home/luis/tradingbot/bot4# /usr/local/include/quickfix/DataDictionaryProvider.h:55:70: error: ‘ptr::shared_ptr’ has not been declared
bash: /usr/local/include/quickfix/DataDictionaryProvider.h:55:70:: No such file or directory
What am I doing wrong?
You need to provide an implementation of the MyApplication class and compile with the appropriate C++11 flags.
Your MyApplication object typically needs to derive from the FIX::Application class which is a pure virtual class and will require you to implement a series of methods such as onLogon(...) and toApp(...)
If you want to just get your code to compile as a first step you can try using the FIX::NullApplication class (defined in quickfix/Application.h) as your starting application object.
The quickfix library must be compiled with this pre-processor directive HAVE_STD_UNIQUE_PTR
Your application must provide an implementation for the MyApplication class and that implementation must inherit from FIX::Application

STL vector error: Unknown type name

I have this structure:
struct casilla{
int x,y;
};
And these lines of code;
std::vector<std::vector<std::vector<casilla> > > iniciales;
iniciales.resize(7);
Wich give me this error in the g++ compiler:
src/tetromino.cpp:5:1: error: unknown type name 'iniciales'
iniciales.resize(7);
^
src/tetromino.cpp:5:10: error: expected unqualified-id
iniciales.resize(7);
Edit: I modified my code and now it's like this:
std::vector<std::vector<std::vector<casilla> > > iniciales(7);
for(int i=0; i<iniciales.size();i++){
iniciales[i].resize(3);
for(int i=0; i<3;i++)
iniciales[i][j].resize(4);
}
iniciales.at(TTR_CUADRADO){{{0,5},{0,6},{1,5},{1,6}}};
iniciales.at(TTR_LREVES)={{ {0,4},{0,5},{0,6},{1,6} },
{{-1,5},{0,5},{1,5},{1,4}},
{{0,4},{1,4},{1,5},{1,6}},
{{-1,5},{-1,6},{0,5},{1,5}}
};
iniciales.at(TTR_L)={{{0,4}{0,5},{0,6},{1,4}},{{-1,5},{0,5},{1,5},{1,6}},
{{0,4},{0,5},{0,6},{-1,6}},{{-1,4},{-1,5},{0,5},{1,5}}};
iniciales.at(TTR_Z)={{{0,5},{0,6},{1,6},{1,7}},{{0,6},{1,6},{0,7},{-1,7},}};
iniciales.at(TTR_ZREVES)={{{0,6},{0,7},{1,5},{1,6}},{{0,6},{0,7},{-1,6},{1,7}}};
iniciales.at(TTR_I)={{{0,4},{0,5},{0,6},{0,7}},{{-2,6},{-1,6},{0,6},{1,6}}};
iniciales.at(TTR_T)={{{0,5},{0,6},{0,7},{1,6}}}
for(int i=0; i<iniciales.size();i++){
for(int i=0; i<3;i++)
iniciales[i][j].shrink_to_fit();
}
}
All the TTR* variables are enumerated values between 0 and 6.
This is when the party starts:
c++ -Wall -g -Iinclude -c src/tetromino.cpp -o obj/tetromino.o
src/tetromino.cpp:5:1: error: expected unqualified-id
for(int i=0; i<iniciales.size();i++){
^
src/tetromino.cpp:10:2: error: unknown type name 'iniciales'
iniciales.at(TTR_CUADRADO){{{0,5},{0,6},{1,5},{1,6}}};
^
src/tetromino.cpp:10:11: error: expected unqualified-id
iniciales.at(TTR_CUADRADO){{{0,5},{0,6},{1,5},{1,6}}};
^
src/tetromino.cpp:11:1: error: unknown type name 'iniciales'
iniciales.at(TTR_LREVES)={{ {0,4},{0,5},{0,6},{1,6} },
^
src/tetromino.cpp:11:10: error: expected unqualified-id
iniciales.at(TTR_LREVES)={{ {0,4},{0,5},{0,6},{1,6} },
^
src/tetromino.cpp:16:1: error: unknown type name 'iniciales'
iniciales.at(TTR_L)={{{0,4}{0,5},{0,6},{1,4}},{{-1,5},{0,5},{1,5},{1,6}},
^
src/tetromino.cpp:16:10: error: expected unqualified-id
iniciales.at(TTR_L)={{{0,4}{0,5},{0,6},{1,4}},{{-1,5},{0,5},{1,5},{1,6}},
^
src/tetromino.cpp:18:1: error: unknown type name 'iniciales'
iniciales.at(TTR_Z)={{{0,5},{0,6},{1,6},{1,7}},{{0,6},{1,6},{0,7},{-1,7},}};
^
src/tetromino.cpp:18:10: error: expected unqualified-id
iniciales.at(TTR_Z)={{{0,5},{0,6},{1,6},{1,7}},{{0,6},{1,6},{0,7},{-1,7},}};
^
src/tetromino.cpp:19:1: error: unknown type name 'iniciales'
iniciales.at(TTR_ZREVES)={{{0,6},{0,7},{1,5},{1,6}},{{0,6},{0,7},{-1,6},{1,7}}};
^
src/tetromino.cpp:19:10: error: expected unqualified-id
iniciales.at(TTR_ZREVES)={{{0,6},{0,7},{1,5},{1,6}},{{0,6},{0,7},{-1,6},{1,7}}};
^
src/tetromino.cpp:20:1: error: unknown type name 'iniciales'
iniciales.at(TTR_I)={{{0,4},{0,5},{0,6},{0,7}},{{-2,6},{-1,6},{0,6},{1,6}}};
^
src/tetromino.cpp:20:10: error: expected unqualified-id
iniciales.at(TTR_I)={{{0,4},{0,5},{0,6},{0,7}},{{-2,6},{-1,6},{0,6},{1,6}}};
^
src/tetromino.cpp:21:1: error: unknown type name 'iniciales'
iniciales.at(TTR_T)={{{0,5},{0,6},{0,7},{1,6}}}
^
src/tetromino.cpp:21:10: error: expected unqualified-id
iniciales.at(TTR_T)={{{0,5},{0,6},{0,7},{1,6}}}
^
src/tetromino.cpp:22:1: error: expected unqualified-id
for(int i=0; i<iniciales.size();i++){
^
src/tetromino.cpp:26:1: error: extraneous closing brace ('}')
}
^
src/tetromino.cpp:27:1: error: no template named 'vector'; did you mean
'std::vector'?
vector<casilla> tetromino::Zonstd::aComun(vector<casillstd::a> v1,...
^~~~~~
std::vector
/usr/include/c++/4.2.1/bits/stl_vector.h:162:11: note: 'std::vector' declared
here
class vector : protected _Vector_base<_Tp, _Alloc>
^
src/tetromino.cpp:27:28: error: no member named 'Zonstd' in 'tetromino'
vector<casilla> tetromino::Zonstd::aComun(vector<casillstd::a> v1,...
~~~~~~~~~~~^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
What should I do?
As #Jack is saying, i think it could be because I'm doind it outside any method. Should i make an specific method to set this global variable. Also I prefer to make a global variable rather than creating one each time I call a function.
PS.:Sorry for the "weird" variable names, I'm programming in Spanish. :P
My guess is that you are trying to execute some code outside of a method, eg:
#include <vector>
std::vector<std::vector<std::vector<casilla> > > iniciales;
iniciales.resize(7);
while it should be
#include <vector>
std::vector<std::vector<std::vector<casilla> > > iniciales;
void someMethod() {
iniciales.resize(7);
}
or, even better:
std::vector<std::vector<std::vector<casilla> > > iniciales(7);

Numerical Recipes v3.0 and pthread library in C++11

If I compile the following code with the command g++ -std=c++11 Threads.cpp -lpthread -I../Libs/nr30, where nr30 is the library provided by http://www.nr.com/, I obtain no errors:
#include <chrono>
#include <thread>
int main(void) {
/* ... Sadly, No further code in here was added... */
}
while if I add the #include "nr3.h" header in the same code as above, I obtain the following error:
In file included from /usr/include/c++/4.7/memory:76:0,
from /usr/include/c++/4.7/thread:40,
from random_test.cpp:34:
/usr/include/c++/4.7/ext/concurrence.h:73:5: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.7/ext/concurrence.h:72:5: error: looser throw specifier for ‘virtual const char* __gnu_cxx::__concurrence_lock_error::what() const’
In file included from /usr/include/c++/4.7/ios:40:0,
from /usr/include/c++/4.7/istream:40,
from /usr/include/c++/4.7/fstream:40,
from ../Libs/nr30/nr3.h:10,
from random_test.cpp:29:
/usr/include/c++/4.7/exception:70:25: error: overriding ‘virtual const char* std::exception::what() const noexcept (true)’
In file included from /usr/include/c++/4.7/memory:76:0,
from /usr/include/c++/4.7/thread:40,
from random_test.cpp:34:
/usr/include/c++/4.7/ext/concurrence.h: In member function ‘virtual const char* __gnu_cxx::__concurrence_lock_error::what() const’:
/usr/include/c++/4.7/ext/concurrence.h:72:18: error: expected primary-expression before ‘,’ token
/usr/include/c++/4.7/ext/concurrence.h: At global scope:
/usr/include/c++/4.7/ext/concurrence.h:81:5: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.7/ext/concurrence.h:80:5: error: looser throw specifier for ‘virtual const char* __gnu_cxx::__concurrence_unlock_error::what() const’
In file included from /usr/include/c++/4.7/ios:40:0,
from /usr/include/c++/4.7/istream:40,
from /usr/include/c++/4.7/fstream:40,
from ../Libs/nr30/nr3.h:10,
from random_test.cpp:29:
/usr/include/c++/4.7/exception:70:25: error: overriding ‘virtual const char* std::exception::what() const noexcept (true)’
In file included from /usr/include/c++/4.7/memory:76:0,
from /usr/include/c++/4.7/thread:40,
from random_test.cpp:34:
/usr/include/c++/4.7/ext/concurrence.h: In member function ‘virtual const char* __gnu_cxx::__concurrence_unlock_error::what() const’:
/usr/include/c++/4.7/ext/concurrence.h:80:18: error: expected primary-expression before ‘,’ token
/usr/include/c++/4.7/ext/concurrence.h: At global scope:
/usr/include/c++/4.7/ext/concurrence.h:89:5: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.7/ext/concurrence.h:88:5: error: looser throw specifier for ‘virtual const char* __gnu_cxx::__concurrence_broadcast_error::what() const’
In file included from /usr/include/c++/4.7/ios:40:0,
from /usr/include/c++/4.7/istream:40,
from /usr/include/c++/4.7/fstream:40,
from ../Libs/nr30/nr3.h:10,
from random_test.cpp:29:
/usr/include/c++/4.7/exception:70:25: error: overriding ‘virtual const char* std::exception::what() const noexcept (true)’
In file included from /usr/include/c++/4.7/memory:76:0,
from /usr/include/c++/4.7/thread:40,
from random_test.cpp:34:
/usr/include/c++/4.7/ext/concurrence.h: In member function ‘virtual const char* __gnu_cxx::__concurrence_broadcast_error::what() const’:
/usr/include/c++/4.7/ext/concurrence.h:88:18: error: expected primary-expression before ‘,’ token
/usr/include/c++/4.7/ext/concurrence.h: At global scope:
/usr/include/c++/4.7/ext/concurrence.h:97:5: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.7/ext/concurrence.h:96:5: error: looser throw specifier for ‘virtual const char* __gnu_cxx::__concurrence_wait_error::what() const’
In file included from /usr/include/c++/4.7/ios:40:0,
from /usr/include/c++/4.7/istream:40,
from /usr/include/c++/4.7/fstream:40,
from ../Libs/nr30/nr3.h:10,
from random_test.cpp:29:
/usr/include/c++/4.7/exception:70:25: error: overriding ‘virtual const char* std::exception::what() const noexcept (true)’
In file included from /usr/include/c++/4.7/memory:76:0,
from /usr/include/c++/4.7/thread:40,
from random_test.cpp:34:
/usr/include/c++/4.7/ext/concurrence.h: In member function ‘virtual const char* __gnu_cxx::__concurrence_wait_error::what() const’:
/usr/include/c++/4.7/ext/concurrence.h:96:18: error: expected primary-expression before ‘,’ token
/usr/include/c++/4.7/ext/concurrence.h: At global scope:
/usr/include/c++/4.7/ext/concurrence.h:309:5: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.7/ext/concurrence.h: In destructor ‘__gnu_cxx::__scoped_lock::~__scoped_lock()’:
/usr/include/c++/4.7/ext/concurrence.h:308:22: error: expected primary-expression before ‘,’ token
In file included from /usr/include/c++/4.7/memory:84:0,
from /usr/include/c++/4.7/thread:40,
from random_test.cpp:34:
/usr/include/c++/4.7/backward/auto_ptr.h: At global scope:
/usr/include/c++/4.7/backward/auto_ptr.h:90:12: error: invalid use of non-static data member ‘std::auto_ptr<_Tp>::_M_ptr’
/usr/include/c++/4.7/backward/auto_ptr.h:103:49: error: from this location
/usr/include/c++/4.7/backward/auto_ptr.h:103:56: error: ‘__p’ was not declared in this scope
/usr/include/c++/4.7/backward/auto_ptr.h:103:59: error: expected ‘;’ at end of member declaration
/usr/include/c++/4.7/backward/auto_ptr.h:103:61: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.7/backward/auto_ptr.h:90:12: error: invalid use of non-static data member ‘std::auto_ptr<_Tp>::_M_ptr’
/usr/include/c++/4.7/backward/auto_ptr.h:112:41: error: from this location
/usr/include/c++/4.7/backward/auto_ptr.h:112:48: error: ‘__a’ was not declared in this scope
/usr/include/c++/4.7/backward/auto_ptr.h:112:61: error: expected ‘;’ at end of member declaration
/usr/include/c++/4.7/backward/auto_ptr.h:112:63: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.7/backward/auto_ptr.h:90:12: error: invalid use of non-static data member ‘std::auto_ptr<_Tp>::_M_ptr’
/usr/include/c++/4.7/backward/auto_ptr.h:125:49: error: from this location
/usr/include/c++/4.7/backward/auto_ptr.h:125:56: error: ‘__a’ was not declared in this scope
/usr/include/c++/4.7/backward/auto_ptr.h:125:69: error: expected ‘;’ at end of member declaration
/usr/include/c++/4.7/backward/auto_ptr.h:125:71: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.7/backward/auto_ptr.h:137:7: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.7/backward/auto_ptr.h:155:9: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.7/backward/auto_ptr.h:182:7: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.7/backward/auto_ptr.h:195:7: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.7/backward/auto_ptr.h:211:27: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.7/backward/auto_ptr.h:226:7: error: expected unqualified-id before ‘{’ token
/usr/include/c++/4.7/backward/auto_ptr.h:241:7: error: expected unqualified-id before ‘{’ token
random_test.cpp:126:1: error: expected ‘}’ at end of input
In file included from /usr/include/c++/4.7/memory:84:0,
from /usr/include/c++/4.7/thread:40,
from random_test.cpp:34:
/usr/include/c++/4.7/backward/auto_ptr.h: In constructor ‘std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr<_Tp>::element_type*)’:
/usr/include/c++/4.7/backward/auto_ptr.h:103:39: error: expected primary-expression before ‘,’ token
/usr/include/c++/4.7/backward/auto_ptr.h: In copy constructor ‘std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr<_Tp>&)’:
/usr/include/c++/4.7/backward/auto_ptr.h:112:31: error: expected primary-expression before ‘,’ token
/usr/include/c++/4.7/backward/auto_ptr.h: In constructor ‘std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr<_Tp1>&)’:
/usr/include/c++/4.7/backward/auto_ptr.h:125:39: error: expected primary-expression before ‘,’ token
/usr/include/c++/4.7/backward/auto_ptr.h: In member function ‘std::auto_ptr<_Tp>& std::auto_ptr<_Tp>::operator=(std::auto_ptr<_Tp>&)’:
/usr/include/c++/4.7/backward/auto_ptr.h:136:32: error: expected primary-expression before ‘,’ token
/usr/include/c++/4.7/backward/auto_ptr.h: In member function ‘std::auto_ptr<_Tp>& std::auto_ptr<_Tp>::operator=(std::auto_ptr<_Tp1>&)’:
/usr/include/c++/4.7/backward/auto_ptr.h:154:40: error: expected primary-expression before ‘,’ token
/usr/include/c++/4.7/backward/auto_ptr.h: In member function ‘std::auto_ptr<_Tp>::element_type& std::auto_ptr<_Tp>::operator*() const’:
/usr/include/c++/4.7/backward/auto_ptr.h:181:25: error: expected primary-expression before ‘,’ token
/usr/include/c++/4.7/backward/auto_ptr.h: In member function ‘std::auto_ptr<_Tp>::element_type* std::auto_ptr<_Tp>::operator->() const’:
/usr/include/c++/4.7/backward/auto_ptr.h:194:26: error: expected primary-expression before ‘,’ token
/usr/include/c++/4.7/backward/auto_ptr.h: In member function ‘std::auto_ptr<_Tp>::element_type* std::auto_ptr<_Tp>::get() const’:
/usr/include/c++/4.7/backward/auto_ptr.h:211:19: error: expected primary-expression before ‘,’ token
/usr/include/c++/4.7/backward/auto_ptr.h: In member function ‘std::auto_ptr<_Tp>::element_type* std::auto_ptr<_Tp>::release()’:
/usr/include/c++/4.7/backward/auto_ptr.h:225:17: error: expected primary-expression before ‘,’ token
/usr/include/c++/4.7/backward/auto_ptr.h: In member function ‘void std::auto_ptr<_Tp>::reset(std::auto_ptr<_Tp>::element_type*)’:
/usr/include/c++/4.7/backward/auto_ptr.h:240:36: error: expected primary-expression before ‘,’ token
/usr/include/c++/4.7/backward/auto_ptr.h: At global scope:
/usr/include/c++/4.7/backward/auto_ptr.h:240:36: error: expected unqualified-id at end of input
/usr/include/c++/4.7/backward/auto_ptr.h:240:36: error: expected ‘}’ at end of input
make: *** [rtest] Error 1
Did you encounter the same problem? Thank you for any advice.
It looks like the authors of this library are defining their own exception classes and they forgot to mark the what() method as noexcept when the code is compiled with C++11 (or they are deriving from deprecated exception classes, e.g. TR1). This reproduces the problem:
#include <stdexcept>
struct my_exception : std::exception
{
const char* what() const { return "what"; }
};
int main()
{
}
and this fixes it:
#include <stdexcept>
struct my_exception : std::exception
{
const char* what() const noexcept { return "what"; }
// ^^^^^^^^
};
int main()
{
}
This means you either have to patch the library yourself (if you have access to the full source code) or you send them a bug report.
(you should still consider using -pthread instead of -lpthread, it's much cleaner and avoids further bugs)

initializer list as argument to operator[]

This question is related to the one discussed here.
I try to use an initializer list to create an argument to be passed to operator[].
#include <string>
#include <vector>
struct A {
std::string& operator[](std::vector<std::string> vec)
{
return vec.front();
}
};
int main()
{
// ok
std::vector<std::string> vec {"hello", "world", "test"};
A a;
// error: could not convert '{"hello", "world", "test"}' to 'std::vector...'
a[ {"hello", "world", "test"} ];
}
My Compiler (GCC 4.6.1) complains:
g++ -std=c++0x test.cpp
test.cpp: In function ‘int main()’:
test.cpp:20:8: error: expected primary-expression before ‘{’ token
test.cpp:20:8: error: expected ‘]’ before ‘{’ token
test.cpp:20:8: error: expected ‘;’ before ‘{’ token
test.cpp:20:35: error: expected primary-expression before ‘]’ token
test.cpp:20:35: error: expected ‘;’ before ‘]’ token
Should this be valid C++11?
Interestingly, when using operator() instead of operator[] it works.
Yes, it is valid C++11 and should work in any compliant compiler.
Please note that C++11 support in gcc is quite immature, and this code example will not compile in any 4.6 version, but only in 4.7 svn snapshot versions.