-4

1

In c++14 It works well

//ar.h

#ifndef AR_H
#define AR_H

template<class T>
class ar {
 public:
  ar();
  ~ar();

  int insertD(T value);

 private:

 protected:

};

#endif //AR_H

..

//ar.cpp

#include "ar.h"


template<class T>
int ar<T>::insertD(T value){
  int testInsert = -1;
  //..

  //..

  return testInsert;
}

//main.cpp

#include "ar.h"
#include "ar.cpp"                  

int main(int argc, char **argv){

 arr<int> arrD;
 arrD.insertD(1);//test


 return 0;
}

This is the right way to do this in C++14, only include, ar.cpp only in main.cpp?

UPDATE

After a long time...I decided to change my GNU and test in another OS, my surprise was that both Example 1 and 3 which are the same, they work well in both C ++ 14 and C ++ 11, maybe it's a bug in my compiler.

https://gcc.gnu.org/onlinedocs/gcc-3.0/gcc_3.html

-pedantic Valid ISO C and ISO C++ programs should compile properly with or without this option (though a rare few will require -ansi or a -std option specifying the required version of ISO C). However, without this option, certain GNU extensions and traditional C and C++ features are supported as well. With this option, they are rejected.

-pedantic: used in conjunction with -ansi, this tells the compiler to be adhere strictly to the ANSI standard, rejecting any code which is not compliant.

  • c ++ 14 without pedantic works well
  • c ++ 14 with pedantic works well
  • c ++ 11 without pedantic, does not work
  • c ++ 11 with pedantic works well
Angel Angel
  • 19,670
  • 29
  • 79
  • 105

2 Answers2

3

Remove #include "ar.cpp" from main. You're defining insert two times.

There is no difference between the relevant rules in C++11 and C++14. You did something different between your test runs that has nothing to do with the language version.

I also recommend not calling it .cpp. Conventionally, nothing you include should be called .cpp. Since you need to put template definitions in a header somewhere, people typically either name such headers either .hpp or something like .ipp to make them stand out. But .cpp is just confusing — indeed, it appears to have led you straight into this problem.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Sorry to bother you, but I can not express myself well in English, some think the error is in that I used the include twice in ar.h, and main, but only used it once, just put it to h so that you, have more details thanks for reply – Angel Angel Nov 21 '15 at 12:32
  • I will try to leave the question clearer, although I just wanted to know if it was well done in c ++ 14 that way, because it works for me, and I see that you say some interesting things, and secondly, because it does not work c ++ 11, but I'm going back to test – Angel Angel Nov 21 '15 at 12:32
  • @Angel: No idea what you just said but I told you what's wrong and how to fix it. – Lightness Races in Orbit Nov 21 '15 at 12:44
  • you are very kind, but if you check my question in section 3, is the same as for C ++ 14, but in C ++ 11, I can not, that works in this way, – Angel Angel Nov 21 '15 at 12:54
  • @AngelAngel: Sorry, I do not understand what you are saying. Again, I told you how to fix your program. Did you try the solution I gave you? – Lightness Races in Orbit Nov 21 '15 at 12:54
  • @AngelAngel: You accepted a completely _incorrect_ answer. – Lightness Races in Orbit Nov 21 '15 at 16:54
  • maybe you are right, and the answer is completely wrong, I'm not an expert, just using -pedantic flag, and the code works for c ++ 11, I accepts the response, even taking several downvotes, because I think it is fair, you can test the code if you like.Maybe not the best way, I do not know, but for this particular case, the error I had solve it with the flag. Thanks for your time – Angel Angel Nov 21 '15 at 21:57
  • sorry if this comment "This is funny the answer..." was annoying bothers you, I'll delete it – Angel Angel Nov 21 '15 at 22:04
  • @AngelAngel: It doesn't "bother" me; it is incorrect. – Lightness Races in Orbit Nov 21 '15 at 22:13
  • beg your pardon, You were right, I do not know, what it was the error, but the above template worked with pedantic, but today I had to make another template, and it worked without pendatic in c ++ 11 and C ++ 14, only include .ipp, and did some, template more and worked well, my English is so bad, but I hope you understand me – Angel Angel Dec 04 '15 at 23:36
  • It's almost as if you should have listened to me in the first place. – Lightness Races in Orbit Dec 05 '15 at 14:08
-2

You should not include any .cpp files, just the .h files. Compile all .cpp files seperately and only link them together. Example makefile:

CC=g++
ARGS=-Wall -pedantic
OBJFILES=ar.o
all: $(OBJFILES)
       $(CC) $(ARGS) $(OBJFILES) main.cpp -o my.program
ar.o:
       $(CC) $(ARGS) -c ar.cpp
NexAdn
  • 56
  • 5
  • You missed the reason for including it, which is that this is a function template definition. Your proposal would actually break the program even more. – Lightness Races in Orbit Nov 21 '15 at 12:19
  • I also downvoted you for assuming the OP is using makefiles. A sample makefile is worthless to people using VS. – Puppy Nov 21 '15 at 12:22