2

I'm working on a project for school and I am running into a bit of a problem (error is in the title).

Here is the line of code that runs into the error:

kruskalS[n].nodeList[m].push_back(tempFirstCity);

kruskalS is a struct and nodeList is a vector of type string within the struct and I'm trying to insert tempFirstCity (also a string) into that array.

I could easily be making a basic mistake since I haven't done any programming since April. Any kind of help would be appreciated and I'm willing to post a bit more information from the program if needed.

4 Answers 4

5

A std::string is (sort of) a container of chars. A push_back function is used to add one element to the end of a container. So when you call kruskalS[n].nodeList[m].push_back(tempFirstCity);, you say you are trying to add one element to the end of the string called kruskalS[n].nodeList[m]. So the compiler expects that one element to be a char.

If you know that tempFirstCity is not empty and you want to add the first char from tempFirstCity to the end of kruskalS[n].nodeList[m] (including the case where you know tempFirstCity.size() == 1), you can do

kruskalS[n].nodeList[m].push_back(tempFirstCity[0]);

If you want to add the entire string after any current contents, you can do

kruskalS[n].nodeList[m] += tempFirstCity;

If you expect there are no current contents and/or you want to just replace anything already there with the tempFirstCity string, you can do

kruskalS[n].nodeList[m] = tempFirstCity;
Sign up to request clarification or add additional context in comments.

5 Comments

Similarly, if tempFirstCity.size() > 1, then he could also use kruskalS[n].nodeList[m].append(tempFirstCity); just as well as kruskalS[n].nodeList[m] += tempFirstCity;.
would there be anything better than a vector for holding a list of strings?
@user1868095 : That depends entirely on your specific requirements, but vector is generally the best starting point if no requirements are yet known,.
there shouldn't be a limit, it does need to hold names of Cities if that matters
@user1868095 : Who said 'limit'? Requirements means algorithmic/space complexities for lookup, insertion, deletion, whether random access traversal is needed, etc. These are very fundamental data structure concepts, and are the core differentiaters between different standard library containers.
5

You can use:

std::string::c_str()

It returns a const char *.

4 Comments

where and how should i implement it?
@user1868095: Check the reference and how to use it, then try to apply to your use case. If you do that you will learn something as well.
Based on OP's description I imagine the goal is to add a new element into nodeList, not append it to an existing element.
-1 There is no overload of basic_string<>::push_back that takes a CharT const*, only a CharT.
0

You say nodeList is an array of type string. i.e. std::string nodeList[x] where x is a constant.

Then assigning a new element to that array where m < x is as follows:

kruskalS[n].nodeList[m] = tempFirstCity;


Based on comments:

For appending to end of vector you don't need the index m:

kruskalS[n].nodeList.push_back(tempFirstCity);

For inserting at index m:

vector<string>::iterator itr = nodeList.begin();
for (int i = 0; i < m; i++)
  itr++;
nodeList.insert(itr, tempFirstCity);

4 Comments

I meant to say vector not string, if that changes anything
OK, thats different, then. What do you mean by insert? Do you mean insert somewhere in the middle of the vector, or append to the end?
put it at the end, it's just supposed to be a list of cities
kruskalS[n].nodeList.push_back(tempFirstCity);
-2

In C++, you can use string::c_str() to convert a string to C programming char array..

3 Comments

where and how should i implement it and do you know why this is happening even though i'm using only strings?
Because sometimes C++ need to invoke some C API functions, which need C style string as their function parameter..
@bhuang3 : And how does that relate to the code in the question at all?

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.