problem using vectors.

hello I am currently working with vectors and learning about them and also learning more about passing by reference. I am getting these errors when I try to get a vector to work.

here's my code.

function in another file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Answers::getString(vector<string> &list)
{
    string str;
    fi.open("test.txt");
    if(fi.good())
    {
        while(!fi.eof())
        {
            getline(fi,str);
            cout<<str<<endl;
            list.push_back(str);
        }
    }
    fi.close();
}


code dealing with the vector from my main my main function.

1
2
3
4
5
6
7
8
vector<string> list;
    Answers test;
    test.getLine();
    test.getString(&list);

    for(int i = 0 ; i < list.size() ; i++){
         cout<<list.at(i)<<endl;
       }


the errors I get is this.

D:\Projects\Tester\main.cpp||In function 'int WinMain(HINSTANCE__*, HINSTANCE__*, CHAR*, int)':|
D:\Projects\Tester\main.cpp|26|error: no matching function for call to 'Answers::getString(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >*)'|
D:\Projects\Tester\Answers.h|14|note: candidates are: void Answers::getString()|
D:\Projects\Tester\main.cpp|28|warning: comparison between signed and unsigned integer expressions|
||=== Build finished: 1 errors, 1 warnings ===|


I think it has something to do with the argument or something to do with how i passed the memory address to the function.
Last edited on
Simple. In Answers's Header File, look for "void getString()" and substitute it with "void getString(std::vector<std::string>>)" or "void getString(vector<string>>)"

EXPLANATION:
Your Class Header actually does NOT promises a getString function that accepts a vector of strings. It only promises a getString function that accepts no parameters. Probably you forgot to edit that line when editing your Answers.cpp file.
Last edited on
lol wow.. thanks... with all the new stuff im learning i completely forgot how classes work. :P
ok now im getting another error with my list object..


header file...

void getString(vector<string> *list);


cpp file...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Answers::getString(vector<string> *list)
{
    string str;
    fi.open("test.txt");
    if(fi.good())
    {
        while(!fi.eof())
        {
            getline(fi,str);
            cout<<str<<endl;
            list.push_back(str);
        }
    }
    fi.close();
}


error
D:\Projects\Tester\Answers.cpp||In member function 'void Answers::getString(std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >*)':|
D:\Projects\Tester\Answers.cpp|26|error: request for member 'push_back' in 'list', which is of non-class type 'std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >*'|
||=== Build finished: 1 errors, 0 warnings ===|
Since list is a pointer to vector<string> you need to use:
 
list->push_back(str);
void getString(vector<string> *list);
Why didn't you held the 'older' one? That was a bit faster, and also easier once you get used to it. I mean, this one:
void getString(vector<string> &list);
what do you mean?... explain how it is more officiant?
Last edited on
The immediate error was that you were creating a pointer to vector (which is weird already) and passing that to the function as the argument, but the function was expecting a reference. Then you made the function take a pointer parameter as well, but attempted to call push_back on the pointer rather than on the pointed-to object.

If you're learning pass-by-reference, forget pointers.

Also, your input loop used eof, incorrectly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;

struct Answers {
    void getString(vector<string>& list)
    {
        ifstream fi("test.txt");
        string str;
        while( getline(fi, str) )
        {
            cout << str << '\n';
            list.push_back(str);
        }
    }
};


int main()
{
    vector<string> list;
    Answers test;
//    test.getLine();
    test.getString(list);

    for(size_t i = 0 ; i < list.size() ; ++i) {
         cout << list[i] << '\n';
    }
}
Last edited on
I used the fi.good() function to make shure the file was open and good.. if you dont include this loop you can have errors with your program.

also could you explain how this is diferent more?

I thought you had to use pointers to hold a memory address...
Last edited on
@Recluse
Your "if(fi.good())" was fine, it became unnecessary with proper input loop.

You "while(!fi.eof())" is an error: it checks the stream status before attempting input, which makes no sense: the stream status is changed after the failed input, not before. It also ignores conditions other than end of file.

Most pointers hold memory addresses, yes. The original post said "learning more about passing by reference", which isn't the same as memory addressing.
Last edited on
If you pass by reference, you will have no work with pointers at all, and that's probably the faster way to "work", as, when you pass by reference, nothing gets copied, but when you pass by pointer, a pointer gets copied, and that's a bit slower.

You used fi.good(), yes, but only once. You should loop on fi.good(), (!fi.eof()) and getline. If getline can't get a line then, if will NOT store the 'unreadable' line.

Let me also say: Every variable holds a memory address. Like, declaring a int a, you will always be able to get a's memory location (the pointer). So, you can still pass by reference, and having the same pointer pointing to that object.

If you mean: "Hey, my teacher told me to use Pointers when I have to edit a Variable from an External Function! That's not gonna work!" Well, Not exactly. Again, when you pass by reference, your function copies nothing, but gets the actual pointer and value of the wanted variable. Thus, it's faster, and can be used to edit the variable from your functions.
ok thanks... Where did you guys learn all this stuff? school? or over time?..
cause I have been learning on my own so far and they leave out alot of the really basic stuff and the computer science skills..
Own and other's experience. Never learnt anything about programming from school.
Topic archived. No new replies allowed.