0

I want to split my code into .cpp and .h files but I've never done it with classes. When compiling I get the following errors:

unresolved external symbol "public: void __thiscall Worker::show(void)" (?show@Worker@@QAEXXZ) referenced in function _main 

unresolved external symbol "public: void __thiscall Worker::Worker(void)" (?show@Worker@@QAEXXZ) referenced in function _main

here's my code:

f.h

#pragma once
#include "stdafx.h"
#include<vector>
#include<iostream>
using namespace std;

class Worker
{
public:

    void show();
    Worker();
    int i;
};

f.cpp

#include "stdafx.h"
#include<vector>
#include<iostream>
#include "f.h"
using namespace std;
void Worker::show() {
    cout << i;
}
Worker::Worker()
{
    int i = 0;
}

main.cpp

#include "stdafx.h"
#include<iostream>
#include "f.h"
using namespace std;
int main()
{
    Worker w;
    w.i = 5;
    w.show();
    return 0;
}

What am I doing wrong?

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
K.W.
  • 59
  • 1
  • 10
  • 1
    Did u compile f.cpp ? – HazemGomaa Nov 12 '16 at 20:19
  • I did. (I used Local Windows Debugger in Visual Studio 2015, I believe it compiles every .cpp file in the open project,at least that's what it did before) – K.W. Nov 12 '16 at 20:23
  • Verify that the file `f.cpp` (should probably be named worker.cpp) is in the project list. Also verify that the `f` file is listed in the Linker Command Line. – Thomas Matthews Nov 12 '16 at 20:25
  • @A.W. I don't see any problem in the code you posted.. it must be the way you are building ... – HazemGomaa Nov 12 '16 at 20:27
  • It wasn't in the project list, I don't know why sometimes the files are added automaticaly to the project and sometimes not. Will pay closer attention to this in the future. Thank you! – K.W. Nov 12 '16 at 20:29

0 Answers0