-4

EDIT: Thank you to everyone who answered, I'm sure whatever it is it was anomalistic and it won't kill my program. Even if you're killing it on the forums reputation wise, that doesn't give you the right to condescend people who are just trying to enter the community, so please remember to be duly respectful if you're going to take the time to help. Otherwise who what's the use?

Once, again thank you to everyone who contributed thoughtfully

EDIT: The original code had an inline declaration, which seemed like it was the issue but the linker error still persists after removing it.

As the title suggests, I'm having an issue with a specific function. I keep getting a linking error when calling it from main, despite the rest of the functions defined in the class .cpp working fine. I moved the function to the header file to check if it was just an issue with the function itself but it was not, since it compiled fine then. I'm new to c++ but feel like I understand separating source code between files pretty well. However, I'm completely lost with this one.

Here it is forward declared in the header:

Screen &move(pos r, pos c);

Here it is defined in the associated .cpp:

Screen& Screen::move(pos r, pos c) {

   pos row = r * width;
   cursor = row + c;
   return *this;

}

Both the main source and the associated source files #include the header file, and as I said, the other functions seem to be working fine.

Thank you!

Ezra Goss
  • 124
  • 8
  • You marked this function as `inline`, which means that it should be inserted in the code "as is", not called normally. Refer this: http://en.cppreference.com/w/cpp/language/inline – Ternvein Jul 13 '16 at 18:50
  • Highly related: http://stackoverflow.com/questions/3992980/c-inline-member-function-in-cpp-file – NathanOliver Jul 13 '16 at 18:50
  • Right I understand the inline issue, I had tried that before asking and just forgot to remove it before submitting the question but thank you that's totally my fault for not clarifying – Ezra Goss Jul 13 '16 at 19:00
  • 2
    What is the linker error? – Niall Jul 13 '16 at 19:05
  • We need more info, it seems. Code of header file (including class definition) and linker error would be helpful. – Ternvein Jul 13 '16 at 19:10
  • 1
    Your edit isn't really helpful. Moving targets are hard to answer concisely, neither invalidating existing answers is respectful or useful. If you have a different question ask another one. – πάντα ῥεῖ Jul 13 '16 at 19:15
  • @πάνταῥεῖ I respect that, but if the info is wrong shouldn't I edit it to not confuse people as advised by other answerers? – Ezra Goss Jul 13 '16 at 19:17
  • @Ternvein just updated with the full code – Ezra Goss Jul 13 '16 at 19:17
  • Alright listen guys I'm just trying to find out if I'm missing something and I appreciate those of you that helped but the last couple times I've tried to ask a question and listen to the advice of members I've been berated with unhelpful and unnecessary answers. This isn't really that inviting to someone who is just trying to get the help of the community in getting off the ground. I'll chalk this up to an inline issue and figure it out by myself if it happens again. – Ezra Goss Jul 13 '16 at 19:22
  • And by unhelpful and unnecessary I just mean rude and condescending, not the questions that pointed out my mistakes, which were valid – Ezra Goss Jul 13 '16 at 19:23
  • @EzraGoss _"but the last couple times I've tried to ask a question and listen to the advice of members I've been berated with unhelpful and unnecessary answers"_ It's not our fault actually, but yours. Learn how to [write appropriate questions](http://stackoverflow.com/help/how-to-ask) here, and when asking for compiler or runtime errors **always!!** provide a [MCVE] of your code. Thank you for following our guidelines in future. – πάντα ῥεῖ Jul 13 '16 at 19:28
  • @πάνταῥεῖ I understand that and took your advice from last time to not just dump code, so now I understand the other end of that and will thoroughly check my code beforehand. I'm leaving this as is, and when I come back for another question I'll make sure to be more prepared – Ezra Goss Jul 13 '16 at 19:30

3 Answers3

0

If you provide inline, why aren't you actually inline your definition? Since this is what's expected by the compiler.

There's no point to move inline definitions to a separate translation unit, since each translation unit want's to see the definition inline.


Replace

inline Screen &move(pos r, pos c);

with

inline Screen &move(pos r, pos c) {
   pos row = r * width;
   cursor = row + c;
   return *this;    
}

and remove the definition from the .cpp file.


If you want to have a forward declaration you can do it in your header file like this:

 class Screen {
      // ...
      Screen &move(pos r, pos c);
 };

 inline Screen &Screen::move(pos r, pos c) {
   pos row = r * width;
   cursor = row + c;
   return *this;    
 }
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • yeah I considered doing that for the sake of continuing the program, however removing the inline didn't help the issue and so I'm more concerned with what is causing it – Ezra Goss Jul 13 '16 at 18:52
  • To be clear I also understand that that would be more efficient, I'm just now curious as to what is going on – Ezra Goss Jul 13 '16 at 18:53
  • Is the `.cpp` actually part of the project? If not, we're again going with the dupe. – πάντα ῥεῖ Jul 13 '16 at 18:53
  • Functions defined within a class definition are automatically inlined making this use of `inline ` superfluous. – Captain Obvlious Jul 13 '16 at 18:54
  • I'm sorry can you clarify your question @πάνταῥεῖ? The .cpp is actually included in the project files if that is what you're asking – Ezra Goss Jul 13 '16 at 18:56
  • @πάντα ῥεῖ , `despite the rest of the functions defined in the class .cpp working fine` That's not the problem, it seems. – Ternvein Jul 13 '16 at 19:07
0

When you put the inline keyword on a function declaration, that means a promise to the compiler that the function definition will be seen in every translation unit where it is used. That is, the function definition should be in the header file, not a source file.

Either put the function definition in the header or get rid of the inline marker.

aschepler
  • 70,891
  • 9
  • 107
  • 161
  • I tried removing the inline before asking the question, I should have said that but it's still not working – Ezra Goss Jul 13 '16 at 18:51
  • @EzraGoss: in that case, I suggest you edit the post and remove it there as well, because that's what everyone thinks about first. – lorro Jul 13 '16 at 18:54
  • Show us the code which is not working without the `inline` specifier then. – Ternvein Jul 13 '16 at 18:54
0

The definition is for a member function Screen::move. The declaration is for a free function named move. There is no definition shown for the free function, so unless there's more code that hasn't been disclosed, calling the free function move will produce a linker error.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165