0

trying to use an externally declared class object, but receiving an error.

wager.h

#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

wager.cpp

#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;

mainwindow.cpp

void MainWindow::street1BetRedClicked()
{
    street1.colorBet="Red";
    qDebug()<<"street1Red Clicked";
}

mainwindow.obj:-1: error: LNK2001: unresolved external symbol "class Wager street1" (?street1@@3VWager@@A)

debug is outputting street1Red Clicked successfully

chuckieDub
  • 1,767
  • 9
  • 27
  • 46

4 Answers4

2

Below error message tells you either you haven't linked Wager.cpp file or you have missed implementing one function. In your case you forgot to give function definition to Wager();

error: LNK2001: unresolved external symbol "class Wager street1" (?street1@@3VWager@@A)

To fix this issue, you need to implement Wager() somewhere, either in Wager.cpp or Wager.h. I provide an sample implementation for Wager::Wager()(default constructor: function name is the same as class name and takes 0 argument). Note: Below code also initialize all class members in member initializers list.

Wager::Wager()
:bet(0),
 payout(0),
 didBet(false),
 won(false),
 colorBet("blue"),
 colorResult("blue)
{
}
billz
  • 44,644
  • 9
  • 83
  • 100
0

This is a linker error, not compiler. Link a library containing the required class.

paulius_l
  • 4,983
  • 8
  • 36
  • 42
0

In C++ methods and functions can be "declared" or "defined".

With declaration you trll the compiler that a certain function or object will be available in the program, even without providing for example the actual function body at the moment.

With definition you actually provide the function body or the storage and initialization for an object.

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;
}

For classes things are a bit more complex, because the two concepts are a bit messed up. Providing two definitions for example would be logically bad, but is allowed in C++ if they are absolutely identical token by token and with the same meaning of all tokens.

Also with classes there are implicit methods that are created automatically by default if you don't provide them. For example when you write:

 class Point
 {
     public:
         double x, y;
 };

the compiler automatically completes your code as if you wrote instead

 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()
         {
         }
 };

All those are both declarations and definitions.

If however you provide just the declaration for one of the implicitly provide methods (like you did for the constructor in your class) then the compiler assumes that you want to implement it yourself in a different way and the default definition will not be automatically generated.

This is the reason for your compile error: the default constructor was declared but was not defined and when assembling up the executable the compiler was missing some parts.

Please also note that C++ is a very complex language with a lot of apparently illogical (and sometimes just illogical) parts and is not a good candidate for learning by experimenting. The only reasonable way to learn C++ is to start form a good book and reading it from cover to cover...

Community
  • 1
  • 1
6502
  • 112,025
  • 15
  • 165
  • 265
-1

I had to remove Wager(); from wager.h, then the project can build. Not sure of the reason for this. Does anyone know?

chuckieDub
  • 1,767
  • 9
  • 27
  • 46