I keep getting the C2280 error when I try to add an object to a vector of type class. Below are the files that are giving me the error
'interfaceText::interfaceText(const interfaceText &)': attempting to reference a deleted function"
interfaceText.h
#include<SFML/Graphics.hpp>
#include<vector>
#include<iostream>
#include<math.h>
#include<sstream>
#include<ctime>
#include<cstdlib>
class interfaceText{
private:
std::string createString();
std::ostringstream stringStream;
sf::Text text;
sf::Vector2f position;
sf::Font font;
sf::Color color;
//DEBUG
int currentAngle = 1;
sf::Color generateRandomColors();
public:
sf::Text returnRenderObject();
interfaceText(sf::Vector2f textPosition, sf::Color textColor);
void updateText(float currentangle);//std::string string, sf::Vector2f textPosition, sf::Color textColor);
};
extern std::vector<interfaceText> textArray;
interfaceText.cpp
#include "interfaceText.h"
interfaceText::interfaceText(sf::Vector2f textPosition, sf::Color textColor):position(textPosition),color(textColor){
font.loadFromFile("AvenirNextLTPro-Cn.otf");
text.setString(createString());
text.setPosition(position);
text.setFont(font);
text.setColor(color);
textArray.push_back(*this); //<-Code that causes error?
}
std::string interfaceText::createString() {
std::string TESTSTRING="DEBUG";
return TESTSTRING;
}
void interfaceText::updateText(float currentAngle){//std::string string, sf::Vector2f textPosition, sf::Color textColor) {
text.setString(createString());
position.x = (cos(currentAngle*3.14 / 180)* position.x/2);
position.y = (sin(currentAngle*3.14 / 180)* position.y/ 2);
text.setPosition(position);
text.setColor(generateRandomColors());
//std::cout << text.getPosition().x<<" " << text.getPosition().y <<'\n';
currentAngle+=1;
}
sf::Text interfaceText::returnRenderObject() {
return text;
}
sf::Color interfaceText::generateRandomColors() {
srand(time(NULL));
sf::Color newColor (rand()%255, rand() % 255, rand() % 255,255);
return newColor;
}
An in main.cpp (this it not all of it because I removed code I deemed irrelevant)
#include"interfaceText.h"
#include<vector>
int main(){
interfaceText newText(sf::Vector2f(100, 100), sf::Color(255, 255, 255, 255));
return 0;
}
I am sure that the code that is causing this error (or at least triggering the compiler to give an error message ) is
textArray.push_back(*this);
in the interfaceText.cpp file
There are also some notes given with the error message as shown below:
note: compiler has generated 'interfaceText::interfaceText' here
see reference to function template instantiation 'void std::allocator<_Ty>::construct<_Objty,interfaceText&>(_Objty *,interfaceText &)' being compiled
from the notes I gather that the compiler is trying to add a new ctor for the interfaceText
class but I don't know why
When you do textArray.push_back(*this);
you make a copy of the object. Unfortunately you cannot copy a interfaceText
since it contains a std::ostringstream
. A std::ostringstream
is not copyable so any class that includes it as a member has the default generated copy constructor marked as deleted.
You will either need to make your own copy constructor and construct the std::ostringstream
in there or you can move the instance into the vector since streams are moveable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.