Hello! I'm fairly new to computer science, and I'm working on a program that holds a database in a vector. The data type for the vector though is defined as a class.

My header file has this in it:

class problem {

	string name; //Name of person with the problem
	string cost; //How it'll cost to fix the problem
	string helper; //Name of person helping solve the problem

	problem (string a_name, string a_cost, string a_helper);

The .cpp file that goes with this header has this:

#include "problem.h"

problem::problem (string a_name, string a_cost, string a_helper) {
	name = a_name;
	cost = a_cost;
	helper = a_helper;

But now I'm having trouble defining a vector in the main.cpp part of my program.

What I want is something like:

string person;
string value;
string assistant;

vector <problem> problems.push_back(person, value, assistant);

Note: person, value, and assistant change multiple times with my program.

This code won't work though. Any help I could get to fix this problem would be most appreciated.

Thank you.

10 Years
Discussion Span
Last Post by bugmenot

you don't declare it like that. First just declare the vector object vector <problem> problems; . Then later on you can add classes to it

vector <problem> problems;
problem person;

    // enter each of the attributes of the person object is not shown

    // add a class object to the vector

call the constructor explicitly to create a new temporary object

problems.push_back(problem(person, value, assistant));
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.