Reputation: 73
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
Upvotes: 0
Views: 677
Reputation: 2822
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");
Upvotes: 2
Reputation: 1782
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}}
)
Upvotes: 0