Link to home
Create AccountLog in
Avatar of Chris Jones
Chris JonesFlag for United States of America

asked on

template question can't find class

i have a c++ template program that i am playing with and i can not seem to get it to work can anyone help this is the error.
Error      1      error C2228: left of '.setItem' must have class/struct/union      c:\documents and settings\administrator\my documents\templates\templates\templates.cpp      17      
Error      2      error C2228: left of '.getItem' must have class/struct/union      c:\documents and settings\administrator\my documents\templates\templates\templates.cpp      18      

CLASS FILE
----------------
#ifndef AnyType_H_
#define AnyType_H_
 
#include <iomanip>
#include <iostream>
#include <cassert>
#include<cstdlib>
 
using namespace std;
 
template <class anyType>
class item
{
public:   
     item(anyType any = '0', bool defined = false);
     anyType getItem();
     void setItem(anyType);
       
       
private:     
      bool defined();
      anyType any;   
      
      };
 
 
 
    // template <class anyType>
    //  item <anyType>::item()
    //  {
    //   this->defined = false;
    //   }
        
       template <class anyType> 
       anyType item <anyType>::getItem()
       {
            if (this->defined == false)
               {
                 cout << "This item has not yet been set" << endl;
                 }
                 else
                        return this->any;
                                    }
            
       template <class anyType>
       void item<anyType>::setItem(anyType item)
       {
             this->any = item;
             this->defined = true;
             }
#endif
 
 
CPP FILE
-----------------
#include "AnyType.h"
#include <iostream>
#include <string>
 
using namespace std;
 
int main()
{
 item <int> intItem();
 //item <string> stringItem;
 item <char> charItem();
 
 int variable = 2;
 char test = 'g';
 
 
 intItem.setItem(variable);
 cout << "The value of ATint is: " << intItem.getItem() << endl;   
    
    
 system("pause");   
 return 0;   
}

Open in new window

Avatar of RishadanPort
RishadanPort

In my memory of C++, you must contain the class file with the cpp file for it to work.
So instead of including the AnyType.H file, you will have to include the AnyType.cpp, and put the class definition inside the cpp file. I may be wrong, but I think I encounted this same problem long time ago, and this is how I solved it
Avatar of Chris Jones

ASKER

i was wanting to do a multi file but i tried puting the class infomation in the cpp file and it still gave me the same error on the code i posted

CODE

 intItem.setItem(variable);
Are you sure you included the cpp file and not the h file? The code specified looks ok to me
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ok thanks this works
great help