1

I'm a C++ noob, so please go easy on me if my doubt seems too trivial.

#pragma once

class cube
{
public:
    double getVolume();
    double getSA();
    void setLength(double length);

private:
    double length_;
};

This is my header file cube.h ^^^

#include "cube.h"

double cube::getVolume()
{
    return length_ * length_ * length_;
}

double cube::getSA()
{
    return 6 * length_ * length_;
}

void cube::setLength(double length)
{
    length_ = length;
}

This is cube.cpp and the next is main.cpp

#include "cube.h"
#include <iostream>
int main()
{

    cube c;

    c.setLength(3.48);
    double vol = c.getVolume();
    std::cout << "Vol: " << vol;

    return 0;
}

Question 1: So when I include the implementation in .h file itself without using .cpp, the code runs as expected. But when I separate the .h and .cpp files, I get an error saying: 'undefined reference to cube::setLength(double)', 'undefined reference to cube::getVolume()'' and 'exited with code=1 in 0.47 seconds'.

I ran it on VS Code and just set it up, and just ran a simple cin/cout and a hello world program. Is there something wrong I'm doing with the code itself or is it an error due to VS Code? Do note that this problem occurs only when I separate the .h and .cpp files.

Question 2: Why even separate the .h and .cpp files?

Edit 1: As @KamilCuk pointed out, the problem might be in linking the main.cpp and cube.cpp files, but I have no clue how you 'link' the cpp files for compilation?

Edit 2: I just might be compiling it the wrong way. I usually just click the Code Runner enabled run button on the top right, but I have no clue how to compile two files at once.

rioV8
  • 24,506
  • 3
  • 32
  • 49
user_9
  • 111
  • 8
  • 2
    [workes](https://onlinegdb.com/SyqLXBk1v) for me – yaodav Jul 05 '20 at 12:11
  • Not reproducible. Can you please share your environment details? – sanitizedUser Jul 05 '20 at 12:12
  • `undefined reference to` Check out which files are added to compilation. Looks like `main.cpp` is compiled, but `cube.cpp` is not linked with it. – KamilCuk Jul 05 '20 at 12:17
  • For your second question about separation, you can have the implementation of a class inline (within the header) or not inline (in a cpp). In the first case you gain having only one file at the expense of having to recompile everything that includes this include file, including the entire Precompilation Header on a single change of the header. For templates however, you usually have it in one header file. – Michael Chourdakis Jul 05 '20 at 12:17
  • No idea what you mean by _'include the implementation in .h file itself without using .cpp'_ and how you _'separate'_ them. – CiaPan Jul 05 '20 at 12:22
  • @KamilCuk, that might be it. How do you 'link' two cpp files? – user_9 Jul 05 '20 at 12:47
  • A good practice is to capitalize the class names -> Cube. The code is good – Benjam Jul 05 '20 at 12:55
  • 1
    `How do you 'link' two cpp files?` Well, how do you compile one file? Compile two. – KamilCuk Jul 05 '20 at 12:58
  • @KamilCuk Till now, I've just used Code Runner to run the main.cpp file. I've tried to compile the main and cube cop files from the Ctrl+Shift+P result after typing compile, but it showed 'error compiling'. – user_9 Jul 05 '20 at 13:59
  • The instructions right here tell you how to modify your `tasks.json` to support more than 1 source file: [https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson) – drescherjm Jul 05 '20 at 15:28
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Zoe Jul 05 '20 at 23:15

1 Answers1

0

Try to do this. This worked for me and gave desired result:

g++ main.cpp cube.cpp -I./ -o cube.out

You can implement in .h file inline as well and can be make your code work. But for more complex classes better to separate the implementation in .cpp and .h

Also its a good practice to guard header file for multiple definitions:

#ifndef _CUBE_H_
#define _CUBE_H_

class Cube
{
   public:
    double getVolume();
    double getSA();
    void setLength(double length);

   private:
    double length_;
 };

 #endif //_CUBE_H_

Below link provided details of how to build multiple C++ files using VSCode.

https://dev.to/talhabalaj/setup-visual-studio-code-for-multi-file-c-projects-1jpi

Nandish
  • 1,136
  • 9
  • 16
  • No luck :( It just says 'cc1plus.exe: warning: ./cube.h: not a directory' – user_9 Jul 05 '20 at 14:00
  • 1
    Remove `-I./cube.h`. `-I` takes a directory not a file... Make sure `cube.c` and `main.c` are in the same directory as `.h` file. – KamilCuk Jul 05 '20 at 14:12
  • @KamilCuk So that created some cube.out file, but nothing else happened. But on the bright side, the terminal didn't spit anything out-- not even an error. And yes, all 3 in the same folder. – user_9 Jul 05 '20 at 14:24
  • `-o` specifies the output file. Then execute the file. `./cube.out`. – KamilCuk Jul 05 '20 at 15:01
  • Yes, sorry modify -I./cube.h to -I./ and then compile and execute ./cube.out. I tried on MacOS terminal. It compiled and got desired result. $ ./cube.out Vol: 42.1442 – Nandish Jul 05 '20 at 18:55