1

I have a header file and a cpp file. In the .h file I declared a class and a function with returning a reference:

.h file:

#include <iostream>
class testclass {
    Car &createCar(int x, int y);
}

.cpp file:

#include<testclass.h>
Car testclass:&createCar(int x, int y)
{
  ....
}

But when I try to access the function in the .cpp file I get an error:

Declaration is not compatible

Christophe
  • 68,716
  • 7
  • 72
  • 138
itranger
  • 175
  • 2
  • 13
  • You use `CarClass &` in the header and `Car` in the .cpp file – Sid S Jan 09 '20 at 23:31
  • Thank you for the information, this was a typing mistake! Sorry! – itranger Jan 09 '20 at 23:32
  • Well, *two* typing mistakes, then - both the type and the misplaced `&` – Sid S Jan 09 '20 at 23:33
  • 2
    Also note that `#include ` implies that your header is in the system include path. If you want to search the current directory, use `#include "testclass.h"` – Sid S Jan 09 '20 at 23:35
  • Note that 1) your function is not public; 2) I hope you are knowing what you're doing when you return a reference, since there are many risks associated with this practice. – Christophe Jan 09 '20 at 23:50

2 Answers2

3

The function definition in .cpp file should be compatible with its decleration in .h file so modify it as follows.

.cpp file:

#include"testclass.h"
Car& testclass::createCar(int x, int y)
{
  ....
}

Note that I modified <testclass.h> to "testclass.h". use the brackets only with the built in headers.

Follow the following line if you want to know why you should use "testclass.h" instead of <testclass.h> and which one of them you should use Link

asmmo
  • 6,922
  • 1
  • 11
  • 25
  • A bit more explanation might be nice, code dumps without explanation are rarely useful. – paxdiablo Jan 09 '20 at 23:33
  • @anonymous, note the OP fixed the typo for the return type. – ChrisMM Jan 09 '20 at 23:38
  • 1
    @anonymous "*use the brackets only with the built in headers*" - more accurately, use `<>` for any header whose path is relative to the project's header file search paths, which includes system headers. Use `""` for any header whose path is relative to the source file currently being compiled. – Remy Lebeau Jan 09 '20 at 23:41
  • @RemyLebeau I think he is very beginner so I think what I said is convenient so far. – asmmo Jan 09 '20 at 23:43
  • @anonymous better for the OP to learn it now rather than later, and why it works the way it does. – Remy Lebeau Jan 09 '20 at 23:44
  • 1
    Keep in mind the use of the two variations of `#include` are *guidelines,* all the standard states is that the locations of the headers is implementation defined. The actual guideline (in the standard) is to use `<>` for headers provided by the implementation, nothing to do with where they live in the filesystem (indeed, they don't even *have* to exist as real files). See `C++20 [cpp.include]`. – paxdiablo Jan 09 '20 at 23:54
1

In your .h file, & is not a reference to a function. The & is part of the function's return type. So, the function actually returns a reference to a Car instance. It would help you to understand it if you actually write it that way:

Car& createCar(int x, int y);

As such, in the .cpp file, you need to move the & to the return type to match the declaration:

#include <iostream>

class testclass {
    Car& createCar(int x, int y);
};
#include "testclass.h"

Car& testclass::createCar(int x, int y)
{
  ....
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770