Thread: learning Templates.. having problems..

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    47

    learning Templates.. having problems..

    this code is giving me an error. im still trying to figure it out and getting frustrated..
    seems like a simple problem. here i is.


    Code:
    #include <iostream>
    using namespace std;
    
    
    template <class T>
    class SAList
    {
    	//friend Node<void>;
    	public:
    
    		void f(int number){ cout << "number is: " << number << " !" << endl;}
    };
    
    
    int main()
    {
    	SAList<void> Obj();
    	Obj.f(5);
            return 0;
    }


    Code:
    error C2228: left of '.f' must have class/struct/union

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    does removing the () after Obj, and removing the void help? Don't know if it will forsure but it's worth a shot.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Yeah, remove the (), the are not needed. I don't know what they mean and it doesn't give you an error, but they are only used when you have a constructor and you put a value. Void is fine.

    So this would be ok
    Code:
    #include <iostream>
    using namespace std;
    
    
    template <class T>
    class SAList
    {
    	//friend Node<void>;
    	public:
    		SAList(int i) {;};  //constructor that does nothing
    		void f(int number){ cout << "number is: " << number << " !" << endl;}
    };
    
    
    int main()
    {
    	SAList<void> Obj(1);
    	Obj.f(5);
            return 0;
    }
    So what does declaring SAList<void> Obj() means and it doesn't give an error??

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    47
    thanks guys, that helps..
    It was a dumb mistake!
    guess i didn't get a good nights sleep

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    With the parentheses, the variable declaration became a function declaration. And a function can't have members, which is why the compiler complained that the expression "left of .f" didn't have the proper type.

    Nothing to do with templates. This would have happened if SAList was a simple type, too.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. Queue with templates
    By pobri19 in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2008, 06:06 AM
  3. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  4. Coding Problems
    By RpiMatty in forum C++ Programming
    Replies: 12
    Last Post: 01-06-2002, 02:47 AM
  5. String and char* problems
    By Dreamerv3 in forum C++ Programming
    Replies: 4
    Last Post: 01-03-2002, 08:36 PM