Thread: Vector of Structure

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, of course not.
    Code:
    struct IntStr
    {
        IntStr(int x): i(x) {}
        IntStr(const char* str): s(str) {}
        int i;
        string s;
    };
    Should probably get rid of the vector in the struct, too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    I went ahead and added the constructors:

    Code:
    struct IntStr
    {
        int i;
        IntStr(int x):    i(x)    {}
    
        string s;
        IntStr(const char* y):    s(y)    {}
    
        vector<IntStr> v;
    };
    
    int main()
    {
        IntStr temp;
        temp.i = 123;
        temp.s = "abc";
        temp.v.push_back(789);
        temp.v.push_back("xyz");
    }
    Now, if I don't have
    Code:
    IntStr(int x):    i(x)    {}
    and
    Code:
    IntStr(const char* y):    s(y)    {}
    then the compiler complains at temp.v.

    If I do have them, then
    the compiler says
    no matching function for call to `IntStr::IntStr()'
    candidates are: IntStr::IntStr(const IntStr&)
    IntStr::IntStr(const char*)
    IntStr::IntStr(int)

    at
    Code:
    IntStr temp;

    This is confusing

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Confusing? Nothing of the sort! You lack understanding of the basic rules! And you call yourself a C++ programmer!?! Just kidding
    When you add specialized constructors, the compiler doesn't substitute a default constructor for you, so you'll have to add it manually:
    Code:
    struct IntStr
    {
        IntStr() {}
        IntStr(int x):    i(x)    {}
        IntStr(const char* y):    s(y)    {}
        int i;
        string s;
    };
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    Yeah but I'm still learning things though. I'm a C++ programmer since I use C++, but I never said I was good at it.
    I just take notes as I go along

    Is there a way to do this:

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    using namespace std;
    
    struct IntStr
    {
        IntStr() {}       
    
        int i;
        IntStr(int ii):  i(ii)  {}
    
        string s;
        IntStr(const char* ss):  s(ss)  {}
        
        vector<IntStr> v;
        IntStr(vector<IntStr> vv):  v(vv)  {}
    };
    
    int main()
    {
        IntStr temp; //works
        
        temp.i = 123; //works
        cout << temp.i << endl; //works
    
        temp.s = "abc"; //works
        cout << temp.s << endl; //works
    
        temp.v.push_back(123789); //works
        temp.v.push_back("abcxyz"); //works
        cout << temp.v.size() << endl; //outputs 2 (proof that v is holding the two elements)
    
        //cout << temp.v.at(0) << endl; //Compiler complains here
        //cout << temp.v.at(1) << endl; //Compiler complains here
    
    }
    Thanks

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    • Why are you placing the vector inside the struct?
    • Yes, it's possible by overloading the << operator. But then, which of the two elements would you print? Or would you print both?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I wonder if what you really want is a structure that involves a union. Because it seems like sometimes you want a specific item in the vector to be a string, and at other times an int.

  7. #22
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    Can it be done with a union?

    I was using struct because I noticed the compiler did not complain by doing

    Code:
        vector<IntStr>
    before the struct IntStr was completed.
    I thought this self-reference would allow me to use vectors of both int and string by defining the int and string inside of the struct.

  8. #23
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    In order to overload <<
    would I do something like this:

    Code:
    IntStr operator<<(const int index) //index number with associated element
    {
    	return v.at(index);
    }

  9. #24
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by CPlus View Post
    I thought this self-reference would allow me to use vectors of both int and string by defining the int and string inside of the struct.
    When you chose to use a structure, you're saying you needed both the int and the string version. Hence when you push back something to the vector, it should be the whole structure, not just an integer or just a string.

    I'm saying you could do something like:
    Code:
    class IntStr
    {
        public:
        IntStr (const char* init)
        {
            SetString (init);
        }
    
        IntStr (int init)
        {
            SetInt (init);
        }
    
        IntStr ()
        {
            mTypeInfo = UNION;
        }
    
        bool GetString (const char *&retval)
        {
            if (mTypeInfo == STRING)
            {
                retval = mIntStr.mStr;
                return false;
            }
            else if (mTypeInfo == INTEGER)
            {
                std::ostringstream conv;
                conv << mIntStr.mInt;
                if (conv)
                {
                    SetString (conv.str().c_str());
                    retval = mIntStr.mStr;
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return true;
            }
        }
    
        bool GetInt (int &retval)
        {
            if (mTypeInfo == INTEGER)
            {
                retval = mIntStr.mInt;
                return false;
            }
            else if (mTypeInfo == STRING)
            {
                std::istringstream conv;
                conv.str(mIntStr.mStr);
                int temp;
                conv >> temp;
                if (conv)
                {
                    SetInt (temp);
                    retval = mIntStr.mInt;
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return true;
            }
        }
    
        void SetString (const char *init)
        {
            mTypeInfo = STRING;
            mIntStr.mStr = init;
            return ;
        }
    
        void SetInt (int init)
        {
            mTypeInfo = INTEGER;
            mIntStr.mInt = init;
            return ;
        }
    
        private:
    
        typedef enum { INTEGER, STRING, UNION } MyType;
        MyType mTypeInfo;
    
        union IntStrTag {
            const char *mStr;
            int mInt;
        }
        mIntStr;
    };
    The union comment was more of a source of inspiration, considering the way you want your object to behave. Actually using a union here would be bothersome, as unions cannot contain objects, so you would have to manage a char* string.

    Rather, have your object contain both a string and an int, and have an interface like this one, Let the object treat its value like an int or a string at any given time, just like a union.

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Honestly, I have no idea where you are trying to get with this idea, or even why you would do such a thing. But you were already pointed out, by laserlight, I believe, of a boost class, Boost.Variant, I believe, that would do this for you. Essentially a C++ union.

    Before we do anything else, I think it's better if we ask you what exactly you want to do and why.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    Thanks, it works WhiteFlags.

    How do I use the << operater on the IntStr type?

    I tried this:

    Code:
    IntStr IntStr::operator<< (IntStr argument)
    {
        return argument;
    }
    It fails, but aside from that, the compiler did not complain at the following declarations

    Code:
        IntStr number;
        number = 123;
        IntStr letter;
        letter = "abc";
    Oh and I'm just testing different ways of doing things.
    I do remember about LaserLight's suggestion of Boost::Variant.

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Such as?
    Of course your assignments would work as your constructors take care of that implicit conversion.
    To overloaded <<, remember what it must take. You are placing cout on the left side, which is an ostream and the right is an IntStr, so construct your operator from that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User
    Join Date
    Feb 2010
    Posts
    67

    Post

    Thanks.

    Would it look something like this?

    Code:
    ostream &operator<<(ostream &output, IntStr intstr)
    {
        if (mTypeInfo == INTEGER)
            output << intstr.mInt;
        elif (mTypeInfo == STRING)
            output << intstr.mStr;
        return output;
    }
    
    istream &operator>>(istream &input, IntStr intstr)
    {
        if (mTypeInfo == INTEGER)
            input >> intstr.mInt;
        elif (mTypeInfo == STRING)
            input >> intstr.mStr;
        return input;
    }

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    operator << parameter list is correct.
    m_TypeInfo is undelcared.
    operator >> parameter list is incorrect (why?).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    Registered User
    Join Date
    Feb 2010
    Posts
    67
    Oh I was hoping to relate to the mTypeInfo within the class. Overloading operators must be done outside of the class and referred to with friend, right?

    Is the operator>> incorrect because of the reference (&) before it?
    istream input instead of istream &input ?

    If I ignored inputs for now, how could I make it so that it outputs a string or int within the union depending on the type?
    Would I use
    typeid().name()
    or would that simply return a union type?

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-18-2010, 04:14 AM
  2. Problem Passing Structure Reference To Function
    By soj0mq3 in forum C Programming
    Replies: 9
    Last Post: 04-24-2010, 10:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 05:48 PM

Tags for this Thread