0

I am trying to learn about classes for C++ but I keep running into a linker problem in which I get the following error message:

Undefined symbols for architecture x86_64: "Sally::printCrap()", referenced from: _main in cpp-c74cd5.o "Sally::Sally()", referenced

Here is the relevant code:

Code File #1: cpp.cpp

#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include "Sally.h"

using namespace std;


int main(){

    Sally sallyObject;

    sallyObject.printCrap();

}

Code File #2: Sally.cpp

#include "Sally.h"
#include <iostream>
using namespace std;
Sally::Sally()
{

}

void Sally::printCrap(){    
    cout << "hello" << endl;
}

Code File #3: Sally.h

#ifndef SALLY_H
#define SALLY_H

#pragma once

class Sally
{
public:
    Sally();
    void printCrap();

private:

};

#endif

I tried messing around with the syntax. Looked around and couldn't find any help that seemed related to my issue. Any help is greatly appreciated, I'm sure this type of issue is an easy fix.

  • 1
    We'll need to see the exact commands you are using to compile the two source files and link the two objects together. Also remember that the order of the objects getting linked matters. – David Grayson Nov 29 '22 at 05:36
  • If you are using VSCode remember by default it only builds the active file. The official documentation tells you that and how to modify your `tasks.json` to have it build all files. I have an answer that talks about this problem in linux and multiple directories: [https://stackoverflow.com/questions/70856563/vscode-g-it-isnt-finding-cpp-definition-files/70856902#70856902](https://stackoverflow.com/questions/70856563/vscode-g-it-isnt-finding-cpp-definition-files/70856902#70856902) the same fix can be used for any supported platform provided you are using the default task and not code-runner. – drescherjm Nov 29 '22 at 13:59

0 Answers0