0

I am new to C++ and I can't figure out the problem I am having with importing a class from another file. I am running and compiling on main.cpp below:

#include <iostream>
using namespace std;

#include "deck.h"
using namespace deck_base;

int main() {
  card new_card;
  cout << new_card.val;
}

I also have a deck.h header file and a deck.cpp implementation file, shown below in that order:

//deck.h
#include <iostream>

namespace deck_base {
  class card {
    public:
      card();
      int val = 0;
  };
}
//deck.cpp
#include "deck.h"
using namespace deck_base;

card::card() {
  val = 10;
};

When I try to compile the main.cpp file, I get the following error in console:

Starting build...
/usr/bin/g++ -fdiagnostics-color=always -std=c++11 -g /Users/######/vscode/blackjack/main.cpp -o /Users/######/vscode/blackjack/main
Undefined symbols for architecture arm64:
  "deck_base::card::card()", referenced from:
      _main in main-85016f.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Build finished with error(s).

I have tried just placing the code in the implementation file into the main file, which worked, but I would prefer importing as clutter will eventually become an issue continuing the project. I am using vs code for this, if that matters as well. None of the solutions to this problem that I have found on here or elsewhere on the internet seem to apply in this situation, or they didn't help.

trichedout
  • 11
  • 3
  • 1
    You're not linking with `deck.cpp`. – Brian61354270 Jan 13 '23 at 22:10
  • 1
    You need to compile `main.cpp` and `deck.cpp`, not just `main.cpp` – AspectOfTheNoob Jan 13 '23 at 22:11
  • If this is VSCode [this](https://code.visualstudio.com/docs/cpp/config-linux#_modifying-tasksjson) part of the documentation tells you that by default VSCode builds only the active file and shows you what change you need to make to your tasks.json to have it build all files in your folder. – drescherjm Jan 13 '23 at 22:16

0 Answers0