Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 未解决的外部问题c++;_C++_Qt_Unresolved External - Fatal编程技术网

C++ 未解决的外部问题c++;

C++ 未解决的外部问题c++;,c++,qt,unresolved-external,C++,Qt,Unresolved External,试图使用外部声明的类对象,但收到错误 打赌 #ifndef WAGER_H #define WAGER_H #include <QString> void won_color_bets(int cardsDealt); class Wager { int bet; int payout; public: bool didBet; bool won; QString colorBet; QString colorResult

试图使用外部声明的类对象,但收到错误

打赌

#ifndef WAGER_H
#define WAGER_H
#include <QString>

void won_color_bets(int cardsDealt);


class Wager
{

    int bet;
    int payout;


public:

    bool didBet;
    bool won;
    QString colorBet;
    QString colorResult;

    Wager();
};

extern Wager street1;

#endif // WAGER_H
#如果下注
#定义下注
#包括
无效赢注(int cardsDealt);
阶级赌注
{
int-bet;
国际收支;
公众:
布尔·迪贝特;
布尔元;
QString染色;
QString染色结果;
下注();
};
外围赌街1号;
#endif//下注
下注

#include "wager.h"
#include "deck.h"
#include<QDebug>
#include<QVector>
#include<QList>
#include"mainwindow.h"


Wager street1;
Wager street2;
Wager street3;
Wager street4;
Wager street5;
#包括“wager.h”
#包括“deck.h”
#包括
#包括
#包括
#包括“mainwindow.h”
赌街1号;
赌街2号;
赌街3号;
赌街4号;
赌街5号;
mainwindow.cpp

void MainWindow::street1BetRedClicked()
{
    street1.colorBet="Red";
    qDebug()<<"street1Red Clicked";
}
void主窗口::Street1betradeClicked()
{
街道1.colorBet=“红色”;

qDebug()这是链接器错误,不是编译器错误。请链接包含所需类的库。

我必须删除
Wager()
从wager.h开始,然后项目可以生成。不确定原因。有人知道吗?

下面的错误消息告诉您,要么您没有链接wager.cpp文件,要么您错过了实现一个函数。在您的情况下,您忘了为
wager();

错误:LNK2001:未解析的外部符号“等级赌注街1” (?街道1@@3V管理员@@A)

要解决此问题,您需要在Wager.cpp或Wager.h中的某处实现
Wager()
。 我为
Wager::Wager()
提供了一个示例实现(默认构造函数:函数名与类名相同,并接受0参数)。 注意:下面的代码还初始化了成员初始值设定项列表中的所有类成员

Wager::Wager()
:bet(0),
 payout(0),
 didBet(false),
 won(false),
 colorBet("blue"),
 colorResult("blue)
{
}

< C++ >方法和函数可以“声明”或“定义”。 通过声明您可以告诉编译器某个函数或对象将在程序中可用,即使此时不提供实际的函数体

使用定义实际上可以为对象提供函数体或存储和初始化

extern int foo;           // this is an integer DECLARATION

int foo = 4;              // this is the DEFINITION

double square(double x);  // function DECLARATION

// function DEFINITION
double square(double x) {
    return x*x;
}

类的情况有点复杂,因为这两个概念有点混乱。例如,提供两个定义将是逻辑上的错误,但如果它们是令牌和具有相同含义的所有令牌,则允许在C++中使用。 对于类,如果您不提供隐式方法,则默认情况下会自动创建隐式方法。例如,当您编写:

 class Point
 {
     public:
         double x, y;
 };
编译器会自动完成您的代码,就像您编写代码一样

 class Point
 {
     public:
         double x, y;

         // default constructor
         Point()
         {
         }

         // copy constructor
         Point(const Point& other)
             : x(other.x), y(other.y)
         {
         }

         // assignment operator
         Point& operator=(const Point& other)
         {
             this->x = other.x;
             this->y = other.y;
             return *this;
         }

         // destructor
         ~Point()
         {
         }
 };
所有这些都是声明和定义

但是,如果您只为隐式提供方法之一提供声明(就像您为类中的构造函数所做的那样),那么编译器会假定您希望以不同的方式自己实现它,并且不会自动生成默认定义

这就是编译错误的原因:默认构造函数已声明但未定义,并且在组装可执行文件时编译器缺少某些部分


请注意,C++是一种非常复杂的语言,有很多明显的不合逻辑(有时甚至不合逻辑)。学习C++是一个很好的方法,唯一的合理方法是从A开始,从Cover to Cover商店读它……< /P>是的,我清理并运行QGED,文件是链接,你还没有实现下注::WAGER()?然后我的评论是正确的,你还没有实现WAGER()。如果你声明了一个方法,它也必须被实现,除非它是一个纯虚方法。