Hey all sorry I'm new to c++ and just trying to get my head around vectors of objects etc.. so here is what I'm playing with

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

using namespace std;

class foo {
 public:
	std::string blah;
	std::string blah1;

	foo() { }
	foo(std::string b, std::string b1) :  blah(b), blah1(b1) { }
	~foo() { }

};

class bar {
 private:
	std::vector<foo> fooclass; 

 public:
	bar(std::vector<foo> &fclass) : fooclass(fclass) { }
	~bar() { }

	void baradd(std::string var1, std::string var2)
	{
		fooclass.push_back(foo(var1, var2));
	}

	void barprint()
	{
		for (std::vector<foo>::iterator i = fooclass.begin(); i != fooclass.end(); i++)
		{
			cout << "blah: " << i->blah << endl;
			cout << "blah1: " << i->blah1 << endl;
		}
	}
};

int main()
{
	std::vector<foo> fc;
	fc.push_back(foo("foo", "bar"));

	bar b = new bar(&fc);
	bar.baradd("bar", "foo");
	bar.barprint();

	return 0;
}

this does not work cause first i'm not understanding what i should be passing to bar() can someone point me in the right direction.

thanks in advance.

line 46 does not require use of new because bar is not a pointer. This is all that line 46 needs to do. bar b(fc);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.