tags:

views:

90

answers:

1

I have a very trival option processing function and i need to enter this function every so often. So i cant allocate the vector. I need to be able to add an element to the vector and save it so when i do come back to this function it is still their.

vector<sStruct> * loadFile(char *myTextFile)
{
    myStruct 
        sStruct; 
    vector<myStruct> 
        vectorAddress,
        vectorData;
vectorData = &vectorAddress;
    string 
        feild1, feild2, feild3, feild4;

    ifstream 
        *inFile = new ifstream;

    inFile->open( myTextFile, ios::in );


    if ( !inFile->good() )
    {
         cout << "? File Doesnt Exist! " << endl;
    }

    while ( !inFile->eof() )
    {

// reading a file deliimted  by commas hi,hello,hey,1234
//...
         getline( *inFile, feild1, ',' );
         sStruct.m_1 = field1;
         getline( *inFile, feild2, ',' );
         sStruct.m_2 = field2;
         getline( *inFile, field3, ',' );
         sStruct.m_3; = feild3
         getline( *inFile, feild4 );
         sStruct.m_4 = feield4;

// saving each member of the struct in the vector
    vectorAddress.push_back( sStruct );

    }

    inFile->clear();
    inFile->close();

    cout << vectorAddress.size() << endl;
    delete inFile;  
//
   (*vectorData) = vectorAddress;
    return vectorData;
}

// This function tries despretly to add another element saved in struct member varaible
// to the end of the vector. I need the information from the first function to be here. What i think im trying to do is refer to the same address in memory.


vector<sStruct> *  addElement(vector<sStruct> *vAddElement)
{    
    myStruct sAddElement;  // referring to the same struct.
    vector<sStruct> vectorAddress;
    vAddElement = &vectorAddress;

    cout << "Enter a String: ";
    cin  >> sAddElement.feild1  // save user spec in struct member

    vectorAddress.push_back( sAddElement );
    cout << vectorAddress.size() << endl;
    (*vAddElement) = vectorAddress;
    return vAddElement;
}

And im trying to do this without changing the function signuatures.

+3  A: 

First of all, use references for this type of thing, it reduces uneccessary code and can't be NULL.

onto the issue, to add to a vector, use push_back or some other method that adds to a vector, what you've done is try to assign to the vector:

vector<sStruct> *addElement(vector<sStruct> &vAddElement) {
    myStruct sAddElement;  // referring to the same struct.

    cout << "Enter a String: ";
    cin  >> sAddElement.feild1  // save user spec in struct member

    vAddElement.push_back(sAddElement);
    cout << vectorAddress.size() << endl;

    return &vAddElement; // since you said you must return a pointer (which is silly)
                         // we'll return the address of the object passed in.
}

EDIT: Why are you using pointers so much (at all) there is absolute no need in your program for any sort of dynamic allocation or taking the address of members. usual stack allocation and pass by reference would let you do the same work with half the code (and correctly).

EDIT: Also, your loop is broken, you can't correctly test for EOF until after you attempted a read. It is easier to do something like:

while(getline(file, line)) { /* process line */ }

EDIT:

also this code of yours isn't even close to what you think it does, I'll recomment it for you so you know:

vector<sStruct> *addElement(vector<sStruct> *vAddElement) {    
    myStruct sAddElement;

    // ...

    vector<sStruct> vectorAddress;  // creates a new *vector* on the stack
    vAddElement = &vectorAddress;   // makes vAddElement point to the new vector
                                    // but does *not* effect the vector whose
                                    // address you passed

    // ...

    vectorAddress.push_back( sAddElement ); // adds an element to the new vector
    cout << vectorAddress.size() << endl;   // it's size will always be 1, 
                                            // since you just added the first element

    (*vAddElement) = vectorAddress; // does absolutely nothing, you are assigning 
                                    // the new vector to itself

    return vAddElement;    // erroneously returns a pointer to the new vector
                           // it was allocated on the stack and no longer exists 
                           // after the return, **never do that **
}
Evan Teran
+1 Would have said that but I couldn't quite fathom if that was what was being asked or not.
Ed Woodcock
I have to use pointers for the return types..
Well sorry to hear that, so what you should do it simply "return " Since the address of a reference is equal to the address of what it refers to.
Evan Teran
Also, your loadfile function is very broken as well. You've made it *way* more complicated than it has to be.
Evan Teran