初始化类-未解析的外部符号
Initializing a Class - Unresolved External Symbol
我在初始化类时遇到了问题,搜索到的所有结果都是其他各种问题。
我试图用头文件中的InkSpot *ink;
初始化类,然后当类需要初始化时,我用ink = new InkSpot(this);
<-这一行导致错误。
inkpuppet.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl InkSpot::InkSpot(class QWidget *)" (??0InkSpot@@QEAA@PEAVQWidget@@@Z) referenced in function "private: void __cdecl InkPuppet::testButton(void)" (?testButton@InkPuppet@@AEAAXXZ)
我是C++的新手,在解决课程时遇到了麻烦。这是我的密码。
inkpupt.cpp
#include "inkpuppet.h"
#include "ui_inkpuppet.h"
#include "newdialog.h"
#include "inkspot.h"
#include <Qt>
#include <QtCore>
#include <QtGui>
#include <QtWidgets>
#include <QDialog>
#include <QMainWindow>
InkPuppet::InkPuppet(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::InkPuppet)
{
ui->setupUi(this);
//connect the frame range boxes to the timeslider
connect(ui->lowerFrameBox, SIGNAL(valueChanged(int)), this, SLOT(setMinimum(int)));
connect(ui->upperFrameBox, SIGNAL(valueChanged(int)), this, SLOT(setMaximum(int)));
//connect the menu items
connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(actionNew()));
//connect test
connect(ui->testButton, SIGNAL(clicked()), this, SLOT(testButton()));
}
InkPuppet::~InkPuppet()
{
delete ui;
}
void InkPuppet::setMinimum(int value)
{
ui->timeSlider->setMinimum(value);
}
void InkPuppet::setMaximum(int value)
{
ui->timeSlider->setMaximum(value);
}
void InkPuppet::actionNew()
{
NewDialog *dialog = new NewDialog;
dialog->setModal(true);
dialog->show();
}
void InkPuppet::testButton()
{
ink = new InkSpot(this);
ui->testButton->setText("working");
//ui->paintAreaLayout->addWidget(ink->widget);
//QWidget *widg = new QWidget();
ui->paintAreaLayout->addWidget(ink->widget);
}
inkpuppet.h
#ifndef INKPUPPET_H
#define INKPUPPET_H
#include "inkspot.h"
#include "ink.h"
#include <QMainWindow>
#include <QWidget>
namespace Ui {
class InkPuppet;
}
class InkPuppet : public QMainWindow
{
Q_OBJECT
public:
explicit InkPuppet(QWidget *parent = 0);
~InkPuppet();
InkSpot *ink;
private slots:
void setMinimum(int value);
void setMaximum(int value);
void actionNew();
void testButton();
public:
Ui::InkPuppet *ui;
};
#endif // INKPUPPET_H
inkspot.cpp
#include "inkspot.h"
#include "inkpuppet.h"
#include "ui_inkpuppet.h"
#include <QtCore>
#include <QtGui>
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
void InkSpot::paintEvent(QPaintEvent *event)
{
QFile *brushInput; //takes raw 8 bit grayscale image, 8 bit values only
char *brushProto;
uchar *brushData;
brushInput = new QFile("x:\Development\InkPuppet\brush.raw"); //open the raw file
brushInput->open(QIODevice::ReadOnly);
QDataStream in;
in.setDevice(brushInput);
int size = brushInput->size(); //set size to length of raw file
brushProto = new char[size];
in.readRawData(brushProto, size); //read file into prototype
brushData = new uchar[size];
for(int i = 0; i < size; ++i)
{
brushData[i] = (uchar)brushProto[i]; //copy char to uchar array
}
QImage test(brushData, 128, 128, QImage::Format_Indexed8);
QImage test2(128, 128, QImage::Format_ARGB32);
QVector<QRgb> vectorColors(256); //create color table
for(int c = 0; c < 256; c++)
{
vectorColors[c] = qRgb(c, c, c);
}
test.setColorTable(vectorColors);
for(int iX = 0; iX < 100; ++iX)
{
for(int iY = 0; iY < 100; ++iY)
{
test2.setPixel(iX, iY, qRgba(255 - (qrand() % 100), 0 + (qrand() % 100), 0 + (qrand() % 100), qAbs((int)test.pixel(iX, iY)-255)));
}
}
//final conversion for stencil and color brush
QPixmap testPixmap = QPixmap::fromImage(test2);
QPixmap testPixmap2 = QPixmap::fromImage(test);
QPainter painter(this);
painter.drawPixmap(150, 50, 100, 100, testPixmap);
painter.drawPixmap(50, 50, 100, 100, testPixmap2);
delete[] brushProto;
delete[] brushData;
delete brushInput;
}
inkspot.h
#ifndef INKSPOT_H
#define INKSPOT_H
#include <QObject>
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
#include <QLabel>
namespace Ui {
class InkSpot;
}
class InkSpot : public QWidget
{
Q_OBJECT
public:
explicit InkSpot(QWidget *parent = 0);
~InkSpot();
void draw(QPainter *painter);
QWidget *widget;
QLabel *label;
signals:
public slots:
protected:
void paintEvent(QPaintEvent *event);
private:
Ui::InkSpot *ui;
};
#endif // INKSPOT_H
我正在查看您发布的InkSpot::InkSpot(QWidget *parent)
定义的所有代码,但没有看到它。错误消息告诉您尚未定义它。所以我认为这一定是问题所在。
仅仅把某个东西的声明放在头文件中是不够的,你还必须定义。你在InkPuppet::InkPuppet(QWidget *parent)
中做到了这一点,所以你只需要在InkSpot::InkSpot(QWidget *parent)
中做到这一点。
相关文章:
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- 未解析的外部符号_MsiLocateComponentW@12.
- 在C++中使用 gRPC 时未解析的外部符号
- LibPrivoxy: 未解析的外部符号 __declspec(dllimport) int __stdcall Sta
- 尝试从 XamlApplication 编译 C++/WinRT 空白应用程序时,我收到未解析的外部符号 winrt_make_*
- 我收到错误LNK2001:未解析的外部符号(C++代码)
- 可视抽象类 c++(错误 LNK 2001:未解析的外部符号)
- 如何摆脱C++中未解析的外部符号"private: static char"错误?
- 当 lib 已添加到其他依赖项时,如何在 VS 中调试未解析的外部符号错误
- 仅在少数方法(静态或共享库)中解析的外部符号
- 未解析的外部符号"__declspec(dllimport)与 Spdlog c++ 库
- 函数 _main (OPENGL C++) 中引用的未解析的外部符号 ________
- 错误LNK2019未解析的外部符号"public: __thiscall SLinkList<char>::SLinkList<char>(void)"
- 使用静态 constexpr 成员的未解析外部符号
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- Dlib LNK2001未解析的外部符号USER_ERROR__consistent_build_configurati
- 矛盾的未解决的外部符号+未使用的库与VS2017和FFMPEG 4
- 链接器错误:切换到unicode生成给出:未解析的外部符号WinMain
- 类模板上一个特定函数的未解析外部符号
- LNK2001:未解析的外部符号public:static类std::vector