gpt4 book ai didi

c++ - Qt 测试 : error symbol(s) not found when instantiating object in test project

转载 作者:太空宇宙 更新时间:2023-11-04 12:42:53 24 4
gpt4 key购买 nike

我确定我在原来的类(class) (conferenceview) 中只是遗漏了一些东西,但我无法完全理解遗漏了什么。当我尝试构建我的 tst_conferencepage.cpp 文件(其中包含我的 conferenceview 类)时,当我尝试实例化 conferenceview 类型的对象时,我收到有关未找到体系结构 x86_64 符号的错误消息。

session View .h:

#ifndef CONFERENCEVIEW_H
#define CONFERENCEVIEW_H
#include "constants.h"

class conferenceView
{
QString conference; // Holds the current conference selected by the fan
QString division; // Holds the current division selected by the fan
public:
conferenceView(); // Default Constructor
void setConference(QString); // Sets the current conference of the fan
QString getConference(); // Returns the current conference of the fan
void setDivision(QString); // Sets the current division of the fan
QString getDivision(); // Gets the current division of the fan
QSqlQuery queryConference(QString); // Returns a query for the teams in a specified conference
QSqlQuery queryDivision(QString); // Returns a query for the teams in a specified conference
QSqlQueryModel* populateView(QString, int); // Returns a QSqlQueryModel index to display the queried data to the table view
};

#endif // CONFERENCEVIEW_H

session View .cpp:

#include "conferenceview.h"

conferenceView::conferenceView()
{
this->conference = "";
this->division = "";
}

// Assigns conference to passed in QString
void conferenceView::setConference(QString conference) { this->conference = conference; }

// Returns conference
QString conferenceView::getConference() { return this->conference; };

// Assigns division to passed in QString
void conferenceView::setDivision(QString division) { this->division = division; }

// Returns division
QString conferenceView::getDivision() { return this->division; }


QSqlQuery conferenceView::queryConference(QString conference)
{
this->setConference(conference); // Sets current conference

// Queries database for team names by conference order by team name
QSqlQuery q;
q.prepare("SELECT TeamName "
"FROM TeamInfo "
"WHERE Conference = :conf "
"ORDER BY TeamName");
q.bindValue(":conf", conference);
q.exec();

return q;
}

QSqlQuery conferenceView::queryDivision(QString division)
{
// Sets current division
if( this->conference == "American Football Conference")
this->setDivision(division.prepend("AFC "));
else
this->setDivision(division.prepend("NFC "));

// Queries database for team names by division order by team name
QSqlQuery q;
q.prepare("SELECT TeamName "
"FROM TeamInfo "
"WHERE Division = :div "
"ORDER BY TeamName");
q.bindValue(":div", division);
q.exec();

return q;
}

QSqlQueryModel* conferenceView::populateView(QString str, int id)
{
QSqlQueryModel* qModel = new QSqlQueryModel;

// if id == 0, sets the qModel to the conference teams
if(id == 0)
{
this->setConference(str);
qModel->setQuery(this->queryConference(this->getConference()));
}
// if id == 1, sets the qModel to the division teams
else
{
this->setDivision(str);
qModel->setQuery(this->queryDivision(this->getDivision()));
}

// Sets the header title of the table view
qModel->setHeaderData(0, Qt::Horizontal, QObject::tr("Team Name"));

return qModel;
}

tst_conferencepage.cpp:

#include <QtTest>
#include "../NFL_Teams_App/conferenceview.h"

class ConferencePage : public QObject
{
Q_OBJECT
QVector<QString> AFC;
public:
ConferencePage();
private slots:
void testAFCConference();
};

ConferencePage::ConferencePage()
{
this->AFC = {"Baltimore Ravens", "Buffalo Bills", "Cincinnati Bengals",
"Cleveland Browns", "Denver Broncos", "Houston Texans",
"Indianapolis Colts", "Jacksonville Jaguars", "Kansas City Chiefs",
"Los Angeles Chargers", "Miami Dolphins", "New England Patriots",
"New York Jets", "Oakland Raiders", "Pittsburgh Steelers", "Tennessee Titans"};
}

void ConferencePage::testAFCConference()
{
    conferenceView c;
QSqlQuery query = c.queryConference("American Football Conference");
int index = 0;
while(query.next())
QVERIFY(this->AFC.at(index) == query.value(index));
}

ConferencePage::testAFCConference() 中的阻塞代码是我实例化 conferenceView 类型的对象的地方,错误源于此。

单元测试.pro:

QT += core testlib sql
QT -= gui

TARGET = UnitTests

CONFIG += qt console warn_on depend_includepath testcase
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += \
tst_conferencepage.cpp

项目层次结构:

完整错误信息:

Undefined symbols for architecture x86_64:
"conferenceView::queryConference(QString)", referenced from: ConferencePage::testAFCConference() in tst_conferencepage.o "conferenceView::conferenceView()", referenced from: ConferencePage::testAFCConference() in tst_conferencepage.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) make[1]: * [UnitTests] Error 1 make: * [sub-UnitTests-make_first] Error 2 01:21:30: The process "/usr/bin/make" exited with code 2. Error while building/deploying project NFLTeamsProject (kit: Desktop Qt 5.11.2 clang 64bit) When executing step "Make"

感谢任何帮助,提前致谢!

最佳答案

您的 UnitTest.pro 文件中缺少 conferenceview.cppconferenceview.h,因此链接器不知道,什么你的测试应该是从构建的。您的 .pro 文件应如下所示:

QT += core testlib sql
QT -= gui

TARGET = UnitTests

CONFIG += qt console warn_on depend_includepath testcase
CONFIG -= app_bundle

TEMPLATE = app

VPATH = "../NFL_Teams_App"

SOURCES += \
tst_conferencepage.cpp \
conferenceview.cpp

HEADERS += \
conferenceview.h

关于c++ - Qt 测试 : error symbol(s) not found when instantiating object in test project,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53258694/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com