Storing class objects in a vector declared in another class is not working

Hi,

i don't understand what i am doing wrong here.
i declared 2 classes A and B. B is supposed to hold a vector of objects of class A.

But when i try to add an object of class A into the vector defined in class B, i get an error: C2280: 'A::A(const A&)': attempting to reference a deleted function
1
2
3
4
5
6
7
8
9
10
11
12
13
class A
{
 //constructor defined
}

class B {
   //constructor defined
   std::vector<A> classAvector;
   void addObject (A classAObj)
   {
     classAvector.push_back(classAObj); //this is not working. 
   }
}


thanks
Last edited on
Try adding a semi-colon to the end of line 8.
the semi-colon is not the problem.
Line 11 is causing the error: "C2280: 'A::A(const A&)': attempting to reference a deleted function."
Last edited on
You’ve posted all but the relevant code.
‘A’ copy constructor could be deleted.

For example, in a situation like this, the code doesn’t compile, but reports a similar error:
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
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <utility>
#include <vector>

class A {
public:
    A(int a_int_arg);
    A(A&& rhs);

private:
    int a_int;
};


A::A(int a_int_arg)
: a_int { a_int_arg }
{
}

A::A(A&& rhs)
    : a_int { std::move(rhs.a_int) }
{
}


class B {
public:
    B() = default;
    void addObject (A class_A_obj);

private:
    std::vector<A> class_A_vector;
};


void B::addObject (A class_A_obj)
{
    class_A_vector.push_back(class_A_obj);
}


int main()
{
    B b;
    A a(13);
    b.addObject(a);
}


Error:
main.cpp:46:18: error: use of deleted function 'constexpr A::A(const A&)'

Compilable in Visual Studio 2017:
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 <vector>

class A
{};

class B
{
public:
   void addObject(A classAObj)
   {
      classAvector.push_back(classAObj);
   }

private:
   std::vector<A> classAvector;

};

int main()
{
   A a;

   B b;

   b.addObject(a);
}
(No output)

By default class methods and data members are set to private access, they can't be used outside the class.

Class definitions require a semi-colon to indicate the end of the class definition.

Providing a compilable sample helps us to help you. What you posted isn't compilable. I had to add headers and a main function.
Last edited on
//classA.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <string>

class A
{
public:
	A();
	A(std::string s1, std::string s1, std::string s3);
	~Note();
	void printAstr();

private:
	std::string _s1;
	std::string _s2;
	std::string _s3;
};


// classA.cpp
1
2
3
4
5
6
7
A::A(void)
{}

A::A(std::string s1, std::string s1, std::string s3)
	: _s1(s1), _s2(s2), _s3(s3)
{}


//classB.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <vector>
#include "classA.h"

class B
{
public:
	B();
	~B();
	void addObject(A classAObj);

private:
	std::vector<A> classAvector;
	
};


//classB.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "classB.h"

B::B()
{
	std::cout << "\n" << "--------" << "\n";
}

B::~B() 
{
	std::vector<A>().swap(classAvector);

}

void B::addObject(A classAObj)
{
	this->classAvector.push_back(classAObj);
}


//main.cpp
1
2
3
4
5
6
7
8
#include "classA.h"
#include "classB.h"

int main()
{
	A objA("hi", "hello", "World");
	B objB();
}


Warning: C4930: B objB(void): prototyped function not called (was a varieble definition intended?)

Error C2280 A::A(const A&): attempting to reference a deleted funtion


Make sure that the code you post compile and still produces the error that you want help with.


Warning: C4930: B objB(void): prototyped function not called (was a varieble definition intended?)
 
B objB();

This declares a function named objB that takes no arguments and returns a B. If you want objB to be an object of type B that is created using the default constructor you can either leave out the parentheses

 
B objB;

or use braces

 
B objB{};
Last edited on
Here is one-file concatenated version. Compiles.
"Fixing" s1->s2 removed most of the errors.
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <string>

class A
{
public:
	A();
	A(std::string s1,
	std::string s2, // fix
	std::string s3);
	//~Note();
	//void printAstr();

private:
	std::string _s1;
	std::string _s2;
	std::string _s3;
};

A::A() // fix
{}

A::A(std::string s1,
std::string s2, // fix
std::string s3)
	: _s1(s1), _s2(s2), _s3(s3)
{}
	
#include <vector>

class B
{
public:
	B();
	~B();
	void addObject(A classAObj);

private:
	std::vector<A> classAvector;
};

#include <iostream> // fix

B::B()
{
	std::cout << "\n" << "--------" << "\n";
}

B::~B() 
{
	std::vector<A>().swap(classAvector);

}

void B::addObject(A classAObj)
{
	this->classAvector.push_back(classAObj);
}
int main()
{
	A objA("hi", "hello", "World");
	B objB; // fix
}

Yes, there might have been a compiler error about constructor, but it was a mere side-effect of primary error.
i thank you all for your replies.

It seems like the error C2280 only occurs when the code is in separated files (.h and .cpp).

What could be the problem?
It's hard to say without seeing the code.
I’ve split keskiverto’s code into 5 files and it compiles and executes without errors or warnings.

ClassA.hpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef CLASS_A_HPP
#define CLASS_A_HPP


#include <string>


class A {
public:
    A() = default;
    A(std::string s1, std::string s2, std::string s3);

private:
    std::string _s1;
    std::string _s2;
    std::string _s3;
};


#endif // CLASS_A_HPP 


ClassA.cpp:
1
2
3
4
5
6
7
#include "ClassA.hpp"

A::A(std::string s1, std::string s2, std::string s3)
    : _s1 { s1 }
    , _s2 { s2 }
    , _s3 { s3 }
{}


ClassB.hpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef CLASS_B_HPP
#define CLASS_B_HPP


#include "ClassA.hpp"
#include <vector>


class B {
public:
    B();
    void addObject(A class_A_obj);
    ~B();

private:
    std::vector<A> class_A_vector;
};


#endif // CLASS_B_HPP 


ClassB.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "ClassB.hpp"
#include <iostream>


B::B()
{
    std::cout << "\n--------\n";
}


B::~B() 
{
    std::vector<A>().swap(class_A_vector);  // ?? To what avail?
}


void B::addObject(A class_A_obj)
{
    class_A_vector.push_back(class_A_obj);
}


main.cpp:
1
2
3
4
5
6
7
8
9
10
#include "ClassB.hpp"
#include <iostream>


int main()
{
    A obj_a("hi", "hello", "World");
    B obj_b;
    obj_b.addObject(obj_a);
}


Compilation instruction:
g++ -std=c++2a -Werror -Wall -Wextra -Wpedantic -O2 ClassA.cpp ClassB.cpp main.cpp -o main.exe


Output:
--------

Well, ok, the output could look a bit scanty :-)


keskiverto’s code makes the code you posted work.
In case it doesn’t apply to the code you did not post, perhaps we should reverse to the first hypothesis (the copy constructor could be deleted).

Topic archived. No new replies allowed.