in Education by
I am trying to write a C++ class in a separate header and cpp file using VS Code as my IDE with the 'run' and 'C++' extensions. main.cpp #include "Fan.h" int main() { Fan fan1; return 0; } Fan.h #ifndef FAN_H #define FAN_H class Fan { public: Fan(); }; #endif Fan.cpp #include "Fan.h" #include using namespace std; Fan::Fan() { cout << "Fan Class" << endl; } I really can't seem to find anything popping out as obviously wrong. I am wondering if it is a setup problem with VS Code. If I change #include "Fan.h" in main.cpp to "Fan.cpp" it works so it makes me think that the code works but that the linker is not setup right. Would appreciate any help! EDIT: Ok so I've tried the code in a different IDE and it worked. It's something to do with VS Code. Here is the error: [Running] cd "c:\Users\<USER>\Desktop\Fan\" && g++ tempCodeRunnerFile.cpp -o tempCodeRunnerFile && "c:\Users\\Desktop\Fan\"tempCodeRunnerFile C:\Users\\AppData\Local\Temp\cclMFKxO.o:tempCodeRunnerFile.cpp:(.text+0x57): undefined reference to `Fan::Fan()' collect2.exe: error: ld returned 1 exit status JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
It sounds like the IDE is only compiling main.cpp. You need to find the command that is compiling main.cpp and ensure that it also compiles fan.cpp into fan.obj. You will also need to ensure that both main.obj and fan.obj are passed to the linker (which produces the executable program, main.exe or whatever). There are two steps involved here: cpp -> obj (compiling each source file into a matching object file) obj -> exe (linking many object files into an executable)

Related questions

0 votes
    I am trying to write a C++ class in a separate header and cpp file using VS Code as my IDE with the 'run' and ' ... #include "Fan.h" #include using namespace std; Fan::Fan() { cout...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    when do we use self in Python? In ruby we don't need to include this whereas in Python we have to, so ... Can someone explain me this? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    I'd like to ensure my RAII class is always allocated on the stack. How do I prevent a class from ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I'd like to ensure my RAII class is always allocated on the stack. How do I prevent a class from ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    A couple months ago I did a project in C++ with Visual Studio that I needed pthreads for, so I ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I was supposed to do this exercise: Write a program in C to print a frequency chart on the screen ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    ________ is rapidly being adopted for computing descriptive and query types of analytics on Big data. (a) EDR (b ... of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
0 votes
    #include struct A { bool f( int a ) { std::cout...
asked Feb 21, 2022 in Education by JackTerrance
0 votes
    I am inputting a 200mb file in my application and due to a very strange reason the memory usage of ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    I am inputting a 200mb file in my application and due to a very strange reason the memory usage of ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    Since I got multiple classes and utility-functions I reuse on a regular basis, I started to create a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 25, 2022 in Education by JackTerrance
0 votes
    Since I got multiple classes and utility-functions I reuse on a regular basis, I started to create a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 24, 2022 in Education by JackTerrance
0 votes
    What is the output file generated by the linker in C?...
asked Nov 9, 2020 in Technology by JackTerrance
0 votes
    I have defined an interface in C++, i.e. a class containing only pure virtual functions. I want ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
...