c++ - function ... has already a body & function template has already been defined -


i have header file:

utility.h:

#pragma once  #include <fstream> #include <iostream> #include <string> #include <vector> #include <windows.h>  using namespace std;  template<std::size_t> struct int_ {};  template <class tuple, size_t pos>  std::ostream& print_tuple(std::ostream& out, const tuple& t, int_<pos>);  struct timer { public:     timer();     void start();     double getmillisec(); private:     large_integer frequency;        // ticks per second     large_integer t1, t2;           // ticks };  //...  #include "utility.cpp" 

and implementation file:

utility.cpp:

#include "utility.h" template <class tuple, size_t pos> std::ostream& print_tuple(std::ostream& out, const tuple& t, int_<pos>) {      ... }  timer::timer() {     // ticks per second     queryperformancefrequency(&frequency); }  void timer::start() {     queryperformancecounter(&t1); }  double timer::getmillisec() {     queryperformancecounter(&t2);     return (t2.quadpart - t1.quadpart) * 1000.0 / frequency.quadpart; } 

which returns error:

error c2995: 'std::ostream &print_tuple(std::ostream &,const tuple &,int_<pos>)': function template has been defined error c2084: function 'timer::timer(void)' has body ... 

the problem not guards (as suggested in this question) since use #pragma once

i`ve read of implementing .tpp files template class implementation, find horrible solution, since visual studio format nothing file.

trying define utility.tpp: (wrong solution)

so substituted #include "utility.cpp #include "utility.tpp in utility.h , define...

utility.tpp

timer::timer() {     // ticks per second     queryperformancefrequency(&frequency); }  void timer::start() {     queryperformancecounter(&t1); }  double timer::getmillisec() {     queryperformancecounter(&t2);     return (t2.quadpart - t1.quadpart) * 1000.0 / frequency.quadpart; } 

and error returned is:

1>memoization.obj : error lnk2005: "public: __cdecl timer::timer(void)" (??0timer@@qeaa@xz) defined in helloworld.obj ... 

this main btw:

helloworld.cpp:

#include <algorithm> #include <stdio.h> #include <stdlib.h> #include <time.h> //#include "memoization.cpp" #include<vector> #include"utility.h"  using namespace std;  int main() { } 

trying define utility.tpp: (right solution, finally)

as in comment made me notice, idiot imported time methods in .tpp file, , should have imported templates function, , in utility.tpp contains print_tuple while utility.cpp contains 3 timer functions.

if going include cpp file header file need exclude cpp file compiling in project. if not header file have cpp file has , when compiler compiles cpp file object have 2 copies of of definitions. 1 in header file has copy of cpp file in , 1 in cpp file compiled. leads redefinition errors.

once remove utility.cpp being compiled other cpp file includes utility.h have copy full source since utility.cpp included.

to remove utility.cpp compiling see: how exclude files visual studio compile?


if reason using cpp file because tpp files not format c++ code c++ can configure msvs treat tpp files c++ files. if go tools -> options -> text editor -> file extensions can add file extension formatting. enter extension in extension box, select microsoft visual c++ editor drop down , click add.

now can use tpp file instead of cpp file , not have remember exclude cpp file build. conform modern practices.


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -