Creating Class Objects and Pushing Back to a Vector

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <vector>

class Tests
{
private:
    int num1, num2;

public:
    Tests();
};

//Default Constructor
Tests::Tests ()
{
    vec.push_back(obj1);
}

int main ()
{
    std::vector <Tests> vec;
    
    Tests obj1;

}
    
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <vector>

class Tests
{
    private:
        int num1 = 0 ;
        int num2 = 99 ;

    public:
        Tests() = default ;
        Tests( int n1, int n2 ) : num1(n1), num2(n2) {}

    friend std::ostream& operator<< ( std::ostream& stm, const Tests& t )
    { return stm << "Tests{" << t.num1 << ',' << t.num2 << '}' ;}
};

int main()
{
    std::vector<Tests> vec ;

    vec.push_back( Tests(11,22) ) ; // push back Tests(11,22)

    // https://en.cppreference.com/w/cpp/container/vector/emplace_back
    vec.emplace_back( 33, 44 ) ; // construct in-place Tests(11,22)

    const Tests x( 55, 66 ) ;
    vec.push_back(x) ;

    const Tests y ; // default constructed Tests(0,99)
    vec.push_back(y) ;

    for( const Tests& t : vec ) std::cout << t << ' ' ; // Tests{11,22} Tests{33,44} Tests{55,66} Tests{0,99}
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/ea50555d6e4419a9
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <vector>

class Tests
{
private:
    int num1, num2;

public:
    Tests();
};

Tests::Tests ()
{
}

int main ()
{
    std::vector <Tests> vec;
  
    vec.push_back(Tests{});
}


But, perhaps closer to what you're asking for would be something like,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#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.

Topic archived. No new replies allowed.