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

5
  • You use CarClass & in the header and Car in the .cpp file Commented Jan 9, 2020 at 23:31
  • Thank you for the information, this was a typing mistake! Sorry! Commented Jan 9, 2020 at 23:32
  • Well, two typing mistakes, then - both the type and the misplaced & Commented Jan 9, 2020 at 23:33
  • 2
    Also note that #include <testclass.h> implies that your header is in the system include path. If you want to search the current directory, use #include "testclass.h" Commented Jan 9, 2020 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. Commented Jan 9, 2020 at 23:50

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

6 Comments

A bit more explanation might be nice, code dumps without explanation are rarely useful.
@anonymous, note the OP fixed the typo for the return type.
@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.
@RemyLebeau I think he is very beginner so I think what I said is convenient so far.
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].
|
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)
{
  ....
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.