0

I have 3 files with the following definitions

car.h

namespace vehicle {

class Car {

    public:
    int wheels;
    char color[];

    void showInfo();

    Car(int wheels) {
        this->wheels = wheels;
    }

};

}

car.cpp

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

void Car::showInfo() {

    cout << this->wheels << endl;

}

prog.cpp (this imports and utilises the above-defined class)

#include "car.h"
using namespace vehicle;

int main() {

    Car c(4);
    c.showInfo();
    return 0;

}

After compiling with g++ prog.cpp this is the error I am getting

/usr/bin/ld: /tmp/ccKlIJ5B.o: in function main': prog.cpp:(.text+0x34): undefined reference to vehicle::Car::showInfo()' collect2: error: ld returned 1 exit status

kaizen
  • 53
  • 6
  • Your compile command should be: `g++ prog.cpp car.cpp` – prehistoricpenguin Aug 06 '21 at 07:09
  • This worked @prehistoricpenguin :). Any documentation related to this I can refer to understand what is happening under the hood? – kaizen Aug 06 '21 at 07:12
  • undefined reference to vehicle::Car::showInfo() : the compiler didn't found the implmentation of this function , so you should compile car.cpp with your your main.cpp : g++ prog.cpp car.cpp -o prog – AdabH Aug 06 '21 at 07:14
  • `char color[];` is not valid C++, and if it is a compiler extension it's probably a port of C99's flexible array members which are non-trivial to use correctly. If you want a string of text just use `std::string`. You should compile with `-Wall -Wextra -pedantic-errors` on GCC to get the best assistance from the compiler. – Quentin Aug 06 '21 at 07:16

0 Answers0