Problem with calling function from header file

I have a problem. I am trying to call a function that I have a prototype for in a header file and an initialization for in a .cpp file. The main function in my main file won't recognize it. It just says: undefined reference to `Car::startCar()'

Main file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  
#include <iostream>
#include "Car.h"

using namespace std;

int main() {
	Car car1;

	car1.startCar();

	return 0;
}


Header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

#ifndef CAR_H_
#define CAR_H_

class Car {
private:

public:
	void startCar();
	Car();
	virtual ~Car();
};

#endif /* CAR_H_ */


.cpp file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "Car.h"

void startCar() { cout << "Car started." << endl;};

Car::Car() {

}

Car::~Car() {

}

.cpp line 4 defines a local function named startCar(). This is unrelated to the function declared in in your class declaration.

To make the function a member of your class, the line should be:
 
void Car::startCar() { cout << "Car started." << endl;};
Last edited on
Thank you AbstractionAnon.
Topic archived. No new replies allowed.