-3

I have a class named variable which has

string name;
double value;

and I have another class named mainwindow (by the way it is qt application.) in main window I have a vector vector<variable> vect I want a class which push_back variable objects with using vect which is under main window I wrote this but it gave me error

class add_variable
{
private:
  variable var;
  MainWindow &vectholder;
public:
  void push_back(var.getname(),var.getvalue());
};

errors

error: 'var' is not a type
error: expected ',' or '...' before '.' token

also I want to reach these variables which is pushed into vector I want to call them within another class

Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
cihad
  • 72
  • 3
  • 9
  • Where is class `variable` declared? And what about `push_back`? – juanchopanza Jun 11 '13 at 18:39
  • I suggest you take your time, and slowly but surely learn C++ from [a good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), because there are here more errors (or bad designs) to makes a simple specific question that suits the Q&A format of Stack Overflow. – Boris Dalstein Jul 06 '13 at 12:41
  • Anyway, I can still comment here: it makes no sense to use a class `add_variable`, containing the variable you want to add in `vectholder.vect`. What you should do instead is to add a *method* to `MainWindow`, that looks like: `MainWindow::push_back(variable var) { vect.push_back(var); }`. – Boris Dalstein Jul 06 '13 at 12:53

1 Answers1

2

You push_back function

void push_back(var.getname(),var.getvalue());

is not right. You should put argument list in the parenthesis. That's why the compiler is expecting a type. What you can do is

void push_back() {
  vectholder.vect.push_back(var);
}
Yang
  • 7,712
  • 9
  • 48
  • 65
  • then how can I get these pushed variables within another class for example if I declare mainwindow a; in another class it will be new object and it wouldnt have pushed variables – cihad Jun 11 '13 at 18:42
  • @CihadYildiz If I understand you correctly, you need a way to pass in a `variable`. You can define a public method `setVar` that takes a `variable` argument and set `var` to that argument. – Yang Jun 11 '13 at 18:49
  • I didnt understand exactly but when I create an object of mainwindow there will be new object so it will have new vector inside it am I wrong or do I misunderstand these class and class object things – cihad Jun 11 '13 at 18:49
  • @CihadYildiz Yes you'll have an empty vector and you need to define how to add things to this vector. – Yang Jun 11 '13 at 18:52
  • I understand adding into vector but how can I get things which is added into vector – cihad Jun 11 '13 at 18:55
  • @CihadYildiz You can define `get` methods in your class for example `variable getVar(int index) { return vectholder[index]; }` and call this method to retrieve what's in your vector holder. – Yang Jun 11 '13 at 18:56
  • @Yang: Note that in this case, `vectholder` is *not* a vector, but contains one. so you should edit your answer and use `vectholder.vect` instead (if I understood the OP question well). – Boris Dalstein Jul 06 '13 at 12:49