0

I'm new in C++ programming and for some time, I've been trying to resolve a problem with a vector<vector<int> >.

This is the header file

#ifndef ASSIGNMENT_H_
#define ASSIGNMENT_H_

#include <iostream>
#include <vector>
#include <string>

using std::string;
using std::vector;
using namespace std;

class Mood{
public:
   string lecture;
   vector<Block> blocks;

   Mood(vector<vector<int> > &m, string lecture){
     this->lecture=lecture;
     for(auto r: m){
        blocks.push_back(Block(r));
     }
  }
};
#endif

Block is another simple class with just 2 int written in the same file (but I think it not important for my problem).

The problem is in the main written in another file:

#include <iostream>
#include <vector>
#include <string>
#include "assignment.h"
using std::vector;
using std::string;
using namespace std;
int main(){
   Mood r= Mood({{1,2}},"Hello");
}

The error is very general: expected expression

Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169

2 Answers2

2

Either you have stripped some important parts of the error message, or your compiler is exceptionally uncommunicative.

The handling of initializer lists is still sometimes a bit - erm - suboptimal. Please try this:

auto main() -> int {
   auto r = Mood({vector<int>{1,2}}, "Hello");
}

Update

In the comments we have found, that you use C++11 initialization syntax, but don't have C++11 support activated. Either activate C++11, or resort to the old approach to initialize vectors:

vector<vector<int> > m;
vector<int> m0;
m.push_back(m0);
m[0].push_back(1);
m[0].push_back(2);
Mood r = Mood(m, "Hello");
cdonat
  • 2,748
  • 16
  • 24
  • I tried your code and: 16:53:54 **** Incremental Build of configuration Debug for project prova **** make all Building file: ../m.cpp Invoking: GCC C++ Compiler g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"m.d" -MT"m.d" -o "m.o" "../m.cpp" ../m.cpp:10:1: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions] auto main() -> int { ^ ../m.cpp:10:13: error: expected function body after function declarator auto main() -> int { ^ 1 warning and 1 error generated. make: *** [m.o] Error 1 16:53:54 Build Finished (took 325ms) – user5591980 Nov 22 '15 at 15:54
  • @user5591980 erm, yes, since you are using brace initialization in your code, I assumed, you use C++11. Now, that I see, you don't, the error message makes sense. Before C++11 `{{1, 2}}` was not an expression. – cdonat Nov 22 '15 at 15:57
  • Switch on C++11 support for g++ with `-std=c++11`. – cdonat Nov 22 '15 at 15:58
0

Try defining the argument m in the constructor as const, e.g.:

Mood(const vector<vector<int> > &m, string lecture)

This will allow you to pass in an R-Value (i.e. {{2,3}})

simpel01
  • 1,792
  • 12
  • 13
  • It works for me! maybe your compiler is quite old and you need to help it. Old compilers had issues figuring out initializer lists, maybe it fixes if you explicitly construct the vector object as follows: `std::vector>({ {1,2}, {2, 3}})` – simpel01 Nov 22 '15 at 15:51
  • Maybe you should try initialize vector without c++11? – Anton Todua Nov 22 '15 at 16:04
  • More infomation about initializing vector of vector: http://stackoverflow.com/questions/17714552/initializing-a-2d-vector-using-initialization-list-in-c11 – Anton Todua Nov 22 '15 at 16:08