1

I need help in my following code and hope that you can help me through. All I wanted is to pass in INT type to setX() and setY(). However, there is no way for me to convert vector char* to int. Is there alternative to this?

template<class T>
vector<string> Delimiter(T inputString){
    int count=0;
    char str[inputString.length()];
    strcpy(str,inputString.c_str());
    char * pch;

    vector<string> returnContainer;

    printf ("Splitting string \"%s\" into tokens:\n",str);
    pch = strtok (str,",[]");
    while (pch != NULL)
    {
        returnContainer.push_back(pch);
        pch = strtok (NULL, " ,[]");
        count++;
    }
    for(int i=0; i<returnContainer.size(); i++){
    cout << "return:" << returnContainer[i] << endl;
    }
    return returnContainer;
}

//Main()

fileDataAfterFiltered = Delimiter(fileData[i]); // Delimiter (vector<string> type)

point2DObj[point2DCount].setX(fileDataAfterFiltered[1]); // error
point2DObj[point2DCount].setY(fileDataAfterFiltered[2]); // error

//Assn3.cpp:107:59: error: no matching function for call to ‘Point2D::setX(std::basic_string&)’

default
  • 11,485
  • 9
  • 66
  • 102
Bryan Wong
  • 623
  • 2
  • 7
  • 21
  • where are the implementations of `setX()` and `setY()`? – default Nov 15 '12 at 11:36
  • Your `setX` or `setY` needs to have an overload that takes `std::string&` because you're passing in an element of a `std::vector`. We don't know what overloads it has, cause you haven't posted that code. – Tony The Lion Nov 15 '12 at 11:37
  • Have a look at [the function atoi()](http://stackoverflow.com/a/7664227/220636). – nabulke Nov 15 '12 at 11:38
  • 1
    @TonyTheLion that's not correct, either in fact or in the advice. all he needs to do is convert a string to an int. – john Nov 15 '12 at 11:39
  • @john, then his question wasn't very clear. – Tony The Lion Nov 15 '12 at 11:39
  • T inputString looks to be defeating T. Where is 'T' being used? – Chubsdad Nov 15 '12 at 11:40
  • @Bryan Wong. Why is Delimiter declared as a template? You aren't even using the templated type. Get rid of `template` and replace `T inputString` with `string inputString`. – john Nov 15 '12 at 11:40
  • Also `char str[inputString.length()];` is a bug, should be `char str[inputString.length() + 1];`. Don't forget that C strings require an extra `char` for the 'null terminator'. – john Nov 15 '12 at 11:42

3 Answers3

1

there's plenty of ways of converting string to int. boost::lexical_cast is one which will magically do the conversion you want. Otherwise you can use atoi (if you don't care about errors), or strtol (if you do).

point2DObj[point2DCount].setX(atoi(fileDataAfterFiltered[1].c_str()));
point2DObj[point2DCount].setX(boost::lexical_cast<int>(fileDataAfterFiltered[1]));
Tom Tanner
  • 9,244
  • 3
  • 33
  • 61
  • Sorry if the question is not phrase correctly. Yes, all I need was to convert vector to int and point2DObj[point2DCount].setX(atoi(fileDataAfterFiltered[1].c_str())); works perfectly fine! – Bryan Wong Nov 15 '12 at 11:47
  • 1
    You mean [`boost::lexical_cast`](http://www.boost.org/doc/libs/1_52_0/doc/html/boost_lexical_cast.html), right? – Benjamin Lindley Nov 15 '12 at 12:28
  • @BenjaminLindley yes, thanks. not used it a lot! updated accordingly – Tom Tanner Nov 15 '12 at 13:37
1

Delimiter() returns a vector<string> and you give one of these strings to setX() and setY(), but both expect an integer parameter. You must convert the string to int

int x = atoi(fileDataAfterFiltered[1].c_str());
point2DObj[point2DCount].setX(x);
int y = atoi(fileDataAfterFiltered[2].c_str());
point2DObj[point2DCount].setY(y);

But: in C++ array and vector elements start at 0 not 1, so you might want to replace this with fileDataAfterFiltered[0] and fileDataAfterFiltered[1] respectively.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
1

If you are using a C++11 compiler, function std::stoi() will do the trick:

point2DObj[point2DCount].setX(std::stoi(fileDataAfterFiltered[1]));

Otherwise you can use the old atoi():

point2DObj[point2DCount].setX(atoi(fileDataAfterFiltered[1].c_str()));

Aside from this, your code has many other problems, but I hope you can fix them by yourself.

Gorpik
  • 10,940
  • 4
  • 36
  • 56