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.
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.
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 ===|
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);
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.
@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.
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..