2

Can someone tell me why I am getting an: "Illegal use of this type of expression: std::string" on the line with the for loop? As far as I can tell everything should be set up correctly. I'm trying to walk through the strings in the vector and check each string member for capitalization (although I'm really only interested in the first one so that the sorting algorithm doesn't separate uppercase words vs. lowercase words...)

/* BiasedSort: accepts vector<string> by REFERENCE and sorts the vector lexographically, except that if the vector 
* contains "Me First", that string is always at the front.
*/

#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;

// getting error on the "for" line.
void ConvertToLower (vector<string> &vector)
{
    for (vector<string>::iterator iter = vector.begin();
        iter != vector.end(); ++iter) {
            string iterString = *iter;
            transform(iterString.begin(), iterString.end(), iterString.begin(), ::tolower);
    }
}

void BiasedSort (vector<string> &vector)
{
    ConvertToLower(vector);
    sort(vector.begin(), vector.end());

}

int main ()
{
    vector<string> myVector;
    myVector.push_back("this");
    myVector.push_back("string");
    myVector.push_back("and");
    myVector.push_back("vector");
    myVector.push_back("are for");
    myVector.push_back("testing");
    myVector.push_back("purposes");
    copy(myVector.begin(), myVector.end(), ostream_iterator<string>(cout, " "));
    cout << endl;

    BiasedSort(myVector);

    copy(myVector.begin(), myVector.end(), ostream_iterator<string>(cout, " "));
    cout << endl;

    system("pause");
    return 0;
}
4
  • Out of curiosity, are you doing the exercises out of the CS106L Course Reader? If so, how useful are you finding them? Commented Jan 29, 2012 at 7:34
  • 1
    abusing namespace std; Just say no. Commented Jan 29, 2012 at 7:35
  • 1. yes I am doing the exercises out of CS106L. The lab is great. Way more C++ specific than the class. Highly recommended. 2. I know I know, namespace std abuse... Call me a newb... Funny. Commented Jan 29, 2012 at 7:38
  • @MCP - You got yourself in trouble partly by using using namespace std. Without that you would have had to prefix the libary items with std::, and everything would have worked fine. Commented Jan 29, 2012 at 8:00

2 Answers 2

3

I believe that the issue here is that you've named the parameter vector:

void ConvertToLower (vector<string> &vector)
{
    for (vector<string>::iterator iter = vector.begin(); /* ... */

Consequently, the compiler is treating the line

vector<string>::iterator

as

((vector < string) > (::iterator))

That is, an expression comparing vector to string, and that result to ::iterator.

To fix this, try renaming the parameter to the function so that you aren't naming the parameter vector. Alternatively, you can fully-qualify the type std::vector to make it explicit that you're creating a std::vector<string>::iterator rather than doing some expression involving the parameter vector.

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

0

Edited: You should not use the name vector as an identifier in this function.

1 Comment

@templatetypedef: that was what I meant. Kind of. Should not. In this particular function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.