Here I wrote some wrong code to tell my problem. I want to create a class object and at the same time, I want it to be pushed back into the vector. As a beginner, I don't know if it's possible. I can't figure out how to do it.
L 16 - The problem is that Tests doesn't know anything about vec and obj1 , so it can't access them.
It makes little or no sense if class Tests would have a vector of Tests object.
You could add obj1 to vec in main.
The saner way would be, in my opinion, to just call push_back from within main. On obj1 or on a temporary. Sometimes cleverness just makes code harder to maintain.
#include <iostream>
#include <vector>
class Tests
{
private:
int num1, num2;
public:
Tests(std::vector<Tests>& vec);
};
Tests::Tests(std::vector<Tests>& vec)
{
vec.push_back(*this); // pushes back a copy of this object
}
int main ()
{
std::vector<Tests> vec;
Tests obj1(vec);
std::cout << vec.size() << '\n';
}
FWIW, if you are trying to make some sort of unit testing framework, I think most frameworks these days still fall back on using hackish macro systems.
e.g. googletest
For JLBorges, thanks for your kind suggestions. I still don't know about streams and this thing is about an assignment and I'm restricted to using what we learn in class. I'll come back there.
For thmm; yes, they don't know about each other. I already said in the question section, that it's a wrong code. And I don't how to make them talk.
For Ganado, I think you are right. I have to think simple sometimes. No, it will be a house area calculator of some kind. For an assignment.