0

When I have a class that's declared completely within a header file (templated types), and that header file is included by multiple cpp source files, compilation errors appear to be reported once per source file that includes the header. So, if I have a header that's used in 8 different places, each error is reported 8 times.

Suggestions? Do I have to live with this, am I doing it wrong, or what?

Edit

Aside from just being generally annoying, this also messes up the error popup when hovering over the red squiggly line in VS. Yay.

enter image description here

(Using Visual Studio 2012)

3Dave
  • 28,657
  • 18
  • 88
  • 151
  • I rarely use visual studio, but could you not precompile the header and then only get the error once? – Silas Dec 10 '13 at 19:42
  • Is that actually an issue? Once the error is fixed, it'll disappear everywhere, no? Are those errors intentional, requiring them to stay there? – jwueller Dec 10 '13 at 19:43
  • @Silas sounds interesting. I wasn't aware that was an option. Off to research. – 3Dave Dec 10 '13 at 19:43
  • @elusive for the most part, it's messy and annoying. – 3Dave Dec 10 '13 at 19:44
  • @DavidLively: Agreed, that is for sure. I was just wondering whether we are dealing with erroneous behavior from the compiler or IDE here. – jwueller Dec 10 '13 at 19:46
  • 2
    You can [Automatically stop Visual C++ 2008 build at first compile error](http://stackoverflow.com/questions/134796/automatically-stop-visual-c-2008-build-at-first-compile-error) – Oswald Dec 10 '13 at 19:50
  • @JasonEnochs yes. The errors are coming from separate cpp files that include the same header. (Verified by selectively removing source files and watching the error count) – 3Dave Dec 10 '13 at 20:03
  • Ok, then using a script like Oswald pointed out above is probably the only way to stop it. – Jason Enochs Dec 10 '13 at 20:07
  • @JasonEnochs if any of y'all would post an answer I'd happily accept it. – 3Dave Dec 10 '13 at 20:09
  • Credit should go to Oswald... or just go to the post he linked you to and up-vote that one. – Jason Enochs Dec 10 '13 at 20:11
  • @OSwald care to post an answer? – 3Dave Dec 10 '13 at 20:11
  • I did post an answer, but stackoverflow turned it into a comment automatically, because it thinks the answer was trivial :( I now voted to close as duplicate instead. – Oswald Dec 10 '13 at 22:22
  • Unfortunately, this isn't really a duplicate. The referenced answer doesn't work for VS2012 as the Macro IDE has been removed. – 3Dave Dec 13 '13 at 17:27

2 Answers2

0

About your declared class, does it contain all you methods' definitions?

Whether it's a yes, then you should declare your functions outside your class and only leave the prototypes within your class, since you seem to be redefining them in each one of your includes causing an error, though you should provide us with more informations concerning the error you get..

//.h
//type as int, double or anything

template<type N>
class Foo
{
    public:

    template<typename T>
    void foo_fct();
};

//.cpp
#include ".h"

template<type N>
template<typename T>
void Foo<N>::foo_fct()
{
    //stuff here
}

this should fix the issue.

Abdellah IDRISSI
  • 530
  • 1
  • 5
  • 16
  • I'm already using that. The problem arises because the header is included by multiple CPP files, which are compiled independently. This isn't a multiple-inclusion problem. – 3Dave Dec 17 '13 at 23:32
  • yeah, then define your methods outside your classes in a .cpp file – Abdellah IDRISSI Dec 17 '13 at 23:52
  • You can define templated members in a CPP file, but you have to jump through hoops to avoid linker errors. When the compiler is evaluating a template, it needs information about the implementation. If it can't get it - for instance, if it's defined in another cpp instead of the header - you'll get linker errors. – 3Dave Dec 19 '13 at 23:22
-1

You're not allowed to include the same file multiple times. E.g

class foo
include "test"

class bar
include "test"

class foobar
include "bar"
include "foo"

foobar will throw errors because it includes bar which will pull in test once then when it attempts to include foo it will attempt to include test again.