I've seen a lot of questions about this, but can't seem to see what I'm missing. I am fairly new at C++. I am using Visual Studio Code with G++ and MINGW32 10.3.0. When I attempt to run test.cpp (below) I receive two errors:
...test.cpp:7: undefined reference to 'QData::getDataPacket(void*)
...undefined reference to 'vtable for QData'
// qdata.h
#ifndef QDATA_H_
#define QDATA_H_
//Define generic queue data
class QData {
private:
int data = 17;
public:
void virtual getDataPacket(void* dataptr);
void virtual setDataPacket(void* dataptr);
};
#endif
// qdata.cpp
#include "qdata.h"
void QData::getDataPacket(void* dataptr) {
*(int*)dataptr = data;
}
void QData::setDataPacket(void* dataptr) {
data = *(int*)dataptr;
}
// test.cpp
#include <iostream>
#include "qdata.h"
int main() {
QData qqq;
int a;
qqq.getDataPacket(&a);
std::cout << a << std::endl;
return 0;
}
I know the code works because it was originally all in one file and compiled fine. From my research this is maybe a linking issue? Most questions related to this refer to needing to define your virtual functions, but I have already done that.
If I use the following command in the terminal, binary.exe runs correctly (the output is 17):
g++ -o binary test.cpp qdata.cpp
Is there a way to get this to compile and run correctly without manually typing in a list of cpp files?
Edit: Since there seems to be some confusion, typically in VSCode you can compile and debug in one go by pushing F5. This is the part where I get the errors above. I am hoping someone can help me understand why that is failing and how to fix it so I can continue testing/debugging in VSCode.
Edit: I'm still voting that this question is unique since I was simply following the trace of the compile error in VS Code. I had actually found this article before, and it did not solve my problem. It is also extremely dense and as a beginner was difficult to understand how it might explain my problem. I will add a visual studio code tag to help people find this question. But every other reference to the vtable error I found was to do with the vtable itself and not following the troubleshooting path to a solution in VS Code.