Simple parameter mismatch error

This is a discussion on Simple parameter mismatch error within the C++ Programming forums, part of the General Programming Boards category; I have a class called File with the following method: Code: template <class T> bool File::writeBytes(T *obj) { if(fwrite(obj, sizeof(T), ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    224

    Simple parameter mismatch error

    I have a class called File with the following method:
    Code:
    template <class T>
    bool File::writeBytes(T *obj)
    {
     if(fwrite(obj, sizeof(T), 1, file) < 1)
      return false;
      
     return true;
    }
    I call the method as follows:
    Code:
    class foo
    {
     public:
      int a;
      string b;
    };
    
    ...
    
     File file("test");
     foo f;
     f.a = 4545;
     f.b = "32453452354";
     file3.writeBytes(&f);
    But I get the following error when compiling:
    /usr/people/cs/student/ymalik/axesone/main.cc:30: undefined reference to `bool File::writeBytes<foo>(foo*)'

    But I do have it since the method is templated. Can someone please tell me what is wrong?

    Thanks.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Is the function defination in the header file or in another source file that you are linking in?

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Well, here's my class definition:

    Code:
    #ifndef _FILE_H_
    #define _FILE_H_
    
    #include <stdio.h>
    #include <string>
    
    using namespace std;
    
    class File
    {
     private:
      FILE *file;
      string fileName;
    
     public:
      File(string);
      File(string, string);
      ~File();
      bool readLine(string &);
      
      template <class T>
      void readBytes(T *);
      
      bool writeLine(string);
      
      template <class T>
      bool writeBytes(T *);
    
      bool reName(string);
      bool isThere();
    };
    
    #endif
    I have .h file with the definition, a .cc file with the implementation, and a tester program.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Try putting all the template function definations in the header. Most compilers require this.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Ok, thanks, that worked, but why did the compiler complain in the tester program and not the class file?

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Because it didn't run into a problem until it tried to write the function and link it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 10:15 PM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 04:23 PM

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