Thread: returning different datatypes

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    44

    returning different datatypes

    I have a class that makes use of one of two different vectors I declared in the class declaration depending on a datatype that has to be specified to the constructor.

    Here are the two declarations:
    Code:
    std::vector<int> intVector;
    std::vector<char> charVector;
    Most of the functions I use to perform operations on these vectors are common or overloaded. This allows the type to be specified at the creation of the object.

    One thing I can't figure out is how to return either int or char, depending on the type in use, without having the implementer of my class have to decide which function to use.

    For example, if someone invokes the return function as follows:
    Code:
    myClass::retVal();
    I want to return an int value from my intVector if the type in use is int, and if the type in use is char I want to return a char value from my charVector.

    Any ideas?
    Check out all my dimensions:
    Height, width, and for a limited time only... Depth!
    -sb

  2. #2
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    You cannot overload functions simply by their return types. It would be ambiguous. I suggest you code up separate return functions.

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    I think this should give you an idé how to solve it if I understand you correctly

    Code:
    tempate <class Object>
    class myClass
    {
    public:
    Object retVal(int pos);
    private:
    std::vector<Object> anyVector;
    };
    I leave the implementation for you
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    44
    Yes, I know you can't overload functions like that. That's why I posted. I couldn't make different functions. What if I decided to support six different datatypes? Then the person using my class would have to a) remember which datatype he specified, and b) know the name of the one of those six functions that returns the right type.

    Ripper, I'm not sure how to use that as I've never seen it before. Here's a shortened version of my class declaration:
    Code:
    enum dataType { D_INT, D_CHAR };
    
    class myClass
    {
    public:
        myClass(dataType);
        ~myClass();
    
        void                          retVal();
    
        int                             i_retVal;
        char                          c_retVal;
    
        std::vector<int>       i_vector;
        std::vector<char>    c_vector;
    };
    Check out all my dimensions:
    Height, width, and for a limited time only... Depth!
    -sb

  5. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Iīm using a feature called templates. Templates are used in generic programming which means that different datatypes can be used on the same class/function e.i.stack, queue.

    Here is a link to templates.

    Read up on templates and if you still stuck post again.

    P.S. If I understand you right you will ONLY use one std::vector in your class. Either an int vector OR a char vector.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    44
    Thanks ripper, learning about templates helped out a lot. However, now I'm getting linker errors calling my member functions in main(). They all show the same error with different functions.

    Here it is:

    error LNK2019: unresolved external symbol "public: int __thiscall myClass<int>::length(void)" referenced in function _main

    Any idea why? Do I have to call my functions in a different way now?

    I called this function the same way you always do:
    Code:
    myClass<int> classObj;
    classObj.length();
    Edit:
    Also tried this:
    Code:
    myClass<int>* classObj = new myClass<int>;
    classObj->length();
    Last edited by Wick; 09-10-2003 at 08:22 PM.
    Check out all my dimensions:
    Height, width, and for a limited time only... Depth!
    -sb

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Wick, if you use templates, you must put ALL of the template's functions in the header file, not as a separate .CPP file. If a template is defined in the same file as it is used, all its functions must be *defined* before the function call, not simply declared, like most functions.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    44
    Wow, wonder why? Thank you. That's good to know.
    Check out all my dimensions:
    Height, width, and for a limited time only... Depth!
    -sb

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stop GUI Application returning when run
    By DaveHope in forum Windows Programming
    Replies: 7
    Last Post: 06-29-2009, 08:57 PM
  2. Help with struct... not returning correct results.
    By drty2 in forum C Programming
    Replies: 7
    Last Post: 01-18-2009, 11:25 PM
  3. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. C++ Datatypes and OS Datatypes :: C++
    By kuphryn in forum Windows Programming
    Replies: 1
    Last Post: 12-07-2002, 02:06 PM