【发布时间】:2016-12-09 15:22:54
【问题描述】:
当我尝试将对象添加到类类型的向量时,我不断收到 C2280 错误。以下是给我错误的文件
'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;
}
main.cpp 中的一个(这不是全部,因为我删除了我认为不相关的代码)
#include"interfaceText.h"
#include<vector>
int main(){
interfaceText newText(sf::Vector2f(100, 100), sf::Color(255, 255, 255, 255));
return 0;
}
我确定导致此错误(或至少触发编译器给出错误消息)的代码是
textArray.push_back(*this);
在 interfaceText.cpp 文件中
错误信息中还给出了一些注释,如下所示:
note: compiler has generated 'interfaceText::interfaceText' here
see reference to function template instantiation 'void std::allocator<_Ty>::construct<_Objty,interfaceText&>(_Objty *,interfaceText &)' being compiled
从我收集到的注释中,编译器正在尝试为 interfaceText 类添加一个新的 ctor,但我不知道为什么
【问题讨论】:
-
缺少
interfaceText的默认构造函数? -
@πάνταῥεῖ 我之前曾尝试添加一个默认构造函数,但它并没有解决问题。不过感谢您的帮助
标签: c++ vector copy-constructor