Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Having trouble with c++ templates

Having trouble with c++ templates

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestioncsharpvisual-studio
2 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BobInNJ
    wrote on last edited by
    #1

    I have a c++ class that contains the following: template class Poly { public: Poly(int order = 0) { this->order = order; coeffs = new double[order+1]; for( int i = 0;i<=order; i++ ) coeffs[i] = 0; } Poly( int order, T *coeffs); Poly( Poly &poly ); ~Poly() { delete []coeffs; } // other member functions are not shown. friend ostream &operator << ( ostream &out, Poly &poly ); private: // data members not show. }; This class is defined in a file called poly.h. In the file poly.cpp, I have the following function definition: template ostream & operator << ( ostream &out, Poly &poly ) { bool printedCoeff = false; . . . return out; } In a third file called test.cpp, I have: double coeffs[] = { 3, 1, 2, 1 }; Poly p1(3,coeffs), p2; cout << p1 << endl; I am using the Microsoft Visual Studio and I get a linker error because of the statement: cout << p1 << endl; I believe that my definition of the function overloading << is not properly getting instantiated. How do I fix this? Bob

    C 1 Reply Last reply
    0
    • B BobInNJ

      I have a c++ class that contains the following: template class Poly { public: Poly(int order = 0) { this->order = order; coeffs = new double[order+1]; for( int i = 0;i<=order; i++ ) coeffs[i] = 0; } Poly( int order, T *coeffs); Poly( Poly &poly ); ~Poly() { delete []coeffs; } // other member functions are not shown. friend ostream &operator << ( ostream &out, Poly &poly ); private: // data members not show. }; This class is defined in a file called poly.h. In the file poly.cpp, I have the following function definition: template ostream & operator << ( ostream &out, Poly &poly ) { bool printedCoeff = false; . . . return out; } In a third file called test.cpp, I have: double coeffs[] = { 3, 1, 2, 1 }; Poly p1(3,coeffs), p2; cout << p1 << endl; I am using the Microsoft Visual Studio and I get a linker error because of the statement: cout << p1 << endl; I believe that my definition of the function overloading << is not properly getting instantiated. How do I fix this? Bob

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Have alook at: "[34.12] Why can't I separate the definition of my templates class from it's declaration and put it inside a .cpp file?"[^] and also at [34.13] of the same document.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups