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.
}
}
#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.
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
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).