Multiple definitions error for all function definitions in header

This is a discussion on Multiple definitions error for all function definitions in header within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Elysia Header guards guard against including the same file multiple times in the same source file, not ...

  1. #16
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    Header guards guard against including the same file multiple times in the same source file, not against including it into multiple source files (once).
    Right. But I only included it in one source file, not multiple source files.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    See edit.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    6,157
    Quote Originally Posted by Programmer_P View Post
    Well, I am 100% sure that all functions I declared are defined somewhere. And I don't have any static variables in my class.
    When there is a dispute between claims of a programmer and the results of the build process, I would be willing to bet the programmer is mistaken.
    Quote Originally Posted by Programmer_P View Post
    Actually, no I'm not. I only include the header in another header, which is then included in a single source file (and that only once). Also, I have header guards in the generated header file that we're discussing, so I don't see why that would be a problem even if I had included it multiple times.
    So you tell us. However, you're the one withholding information you deem irrelevant, and forcing us to guess what relevant information you've withheld.

    You don't need to provide your complete project, but a SMALL and complete example of code that illustrates your concern will be useful. In the process of creating such an example (cutting out the irrelevant bits and testing) you might even find the actual problem yourself.

    Providing a header file is not enough. You are reporting problems in the build process, and a header file can't usually be compiled in isolation. The problem you have is almost certainly due to some interaction between your header file and the source file (or files, although I note your repeated claim there is only one) that #include it.

    Your header file is at odds with quite a few good coding practices.

    1) NEVER employ "using namespace std;" in a header file. That is an effective way to break code that #include's that header file - often in a manner that is unsolvable. Yes, that means you need to sprinkle "std::" prefixes everywhere.

    2) You have declared member functions of your class as inline. However, it is the definitions (i.e. implementations) of the functions that need to be inline.

    3) It is rather pointless (and also expensive) to throw an exception and then immediately catch it in order to rethrow it.

    4) Your class effectively uses member functions to return references to private members. That is essentially bypassing the benefits of having private members.
    Right 98% of the time, and don't care about the other 3%.

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Quote Originally Posted by grumpy View Post
    2) You have declared member functions of your class as inline. However, it is the definitions (i.e. implementations) of the functions that need to be inline.
    As far as I am aware, you only need to declare a function inline for it to be inline. You don't need the inline keyword in the definition or implementation.
    Thus, Programmer_P's way of defining the functions inline in the class is valid, AFAIK.
    (It being a good idea is another matter.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    Jun 2005
    Posts
    6,157
    Quote Originally Posted by Elysia View Post
    (It being a good idea is another matter.)
    That was actually my point.

    Having little mismatches between declaration and definition is a good way to introduce little mistakes. Such as declaring one thing, but the subsequent definition defines something else.
    Right 98% of the time, and don't care about the other 3%.

  6. #21
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by grumpy View Post
    When there is a dispute between claims of a programmer and the results of the build process, I would be willing to bet the programmer is mistaken.

    So you tell us. However, you're the one withholding information you deem irrelevant, and forcing us to guess what relevant information you've withheld.

    You don't need to provide your complete project, but a SMALL and complete example of code that illustrates your concern will be useful. In the process of creating such an example (cutting out the irrelevant bits and testing) you might even find the actual problem yourself.

    Providing a header file is not enough. You are reporting problems in the build process, and a header file can't usually be compiled in isolation. The problem you have is almost certainly due to some interaction between your header file and the source file (or files, although I note your repeated claim there is only one) that #include it.

    Your header file is at odds with quite a few good coding practices.

    1) NEVER employ "using namespace std;" in a header file. That is an effective way to break code that #include's that header file - often in a manner that is unsolvable. Yes, that means you need to sprinkle "std::" prefixes everywhere.

    2) You have declared member functions of your class as inline. However, it is the definitions (i.e. implementations) of the functions that need to be inline.

    3) It is rather pointless (and also expensive) to throw an exception and then immediately catch it in order to rethrow it.

    4) Your class effectively uses member functions to return references to private members. That is essentially bypassing the benefits of having private members.
    1) Oops...forgot I did that. I'll fix that.
    2) I don't think that's true. It can be written before either or both. But I went ahead and put it before both shortly after attaching the file here.
    3) While that may be, I did that only to get rid of a compiler error saying it expected a catch before the next coding statement. It seems that that is a requirement.
    4) No, my functions all return const references.
    Quote Originally Posted by Elysia View Post
    As far as I am aware, you only need to declare a function inline for it to be inline. You don't need the inline keyword in the definition or implementation.
    Thus, Programmer_P's way of defining the functions inline in the class is valid, AFAIK.
    (It being a good idea is another matter.)
    Quote Originally Posted by grumpy View Post
    That was actually my point.

    Having little mismatches between declaration and definition is a good way to introduce little mistakes. Such as declaring one thing, but the subsequent definition defines something else.
    As mentioned above, I changed it already to have inline before both declarations and definitions.

    And just to clear up another point, I am not ignoring the request for a small compilable example. Its just I'm a little bit busy right now, and it'll have to be sometime later.
    Last edited by Programmer_P; 03-04-2011 at 04:53 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  7. #22
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Quote Originally Posted by Programmer_P View Post
    3) While that may be, I did that only to get rid of a compiler error saying it expected a catch before the next coding statement. It seems that that is a requirement.
    That should not be a requirement. Possibly you did not understand the compiler error or you did something weird?
    You are never required to catch an exception.

    Possibly you also tried to add a try block. Each try block requires at least one catch block. However, you do not need a try block to throw exceptions.
    If you are not going to catch an exception, then don't add a try block.
    Last edited by Elysia; 03-05-2011 at 07:11 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #23
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    That should not be a requirement. Possibly you did not understand the compiler error or you did something weird?
    You are never required to catch an exception.

    Possibly you also tried to add a try block. Each try block requires at least one catch block. However, you do not need a try block to throw exceptions.
    If you are not going to catch an exception, then don't add a try block.
    Oh, ok. I didn't know that. I thought you did need a try block to throw exceptions.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  9. #24
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Ok, it turns out that other error was completely un-related to the original error that I was getting. It was about something else entirely.

    The last error that I mentioned was due to not defining a class function that I declared. I forgot I had declared that one, and it took me some time to figure out how to display the complete linking error in Qt Creator. Once I did, I immediately learned what was causing the error, and fixed it on my own.

    Cheers...and thanks people.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 5 times "local function definitions are illegal"
    By Borislav in forum C++ Programming
    Replies: 24
    Last Post: 01-11-2011, 01:37 AM
  2. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 03:25 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21