0

I'm using vscode and it doesn't compile it's Rectangle.cpp in the main file , when I used clion it compiled successfully when it was .h and did not work .cpp

I use gcc (MinGW.org GCC Build-2) 9.2.0

here is my all 3 files

main:

#include <bits/stdc++.h>
#include "Rectangle.h"

using namespace std;

int main(){
    Rectangle r1(10.1,11.1); 
    cout<<r1.get_length()<<" "<<r1.get_width();
}

Rectangle.h:

#ifndef RECTANGLE_H
#define RECTANGLE_H

#pragma once

class Rectangle
{
public:
    float get_length();
    float get_width();
    Rectangle();
    Rectangle(float l , float w);

private:
    float length;
    float width;
};

#endif

Rectangle.cpp:

#include "Rectangle.h"
#include <iostream>
using namespace std;

Rectangle::Rectangle(){
    
}
Rectangle::Rectangle(float l,float w):length(l) , width(w){

}
float Rectangle::get_length(){
    return length;
}

float Rectangle::get_width(){
    return width;
}
  • 2
    The program works [here](https://onlinegdb.com/kV9sGI_Pi). Also see [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Jason Jul 21 '22 at 12:22
  • 2
    When you build your application, you do remember to build with `Rectangle.cpp`? It is included in your actual project? And note that VSCode doesn't have "projects" like CLion does, but you can still use CMake and similar tools to create build-files that work with VSCode. – Some programmer dude Jul 21 '22 at 12:23
  • 3
    [This VSCode with MinGW documentation](https://code.visualstudio.com/docs/cpp/config-mingw) might be helpful. Pay close attention to the `tasks.json` configuration file, and how it could be used to build projects maintained by e.g. CMake. – Some programmer dude Jul 21 '22 at 12:25
  • Remember that in VSCode by default it builds only the active file. It does not build all cpp files in a folder. The documentation tells you how to get around this limitation if you are using the default method of building with a tasks.json file. If you had previously used CLion you may want to install the CMake tools extension to VSCode and use that with your existing `CMakeLists.txt` in VSCode that way it will build all your files. – drescherjm Jul 21 '22 at 13:33

0 Answers0