![]() |
|
#1
|
|||
|
|||
Changing values at specific elements in a vectorHello,
I have a two dimensional vector(type string) here and I'm looking to swap the values in a specific column within it with the data I have in another vector(type int). It seems I have converted the value of the int vector correctly using stringstream and set that equal to a string variable. However, when I attempt to set the value in the two dimensional vector equal to my string variable, I get the following error: error C2440: '=' : cannot convert from 'std::string' to 'char' Code snippet: CPP / C++ / C Code:
Also, I'm attempting to pass this function a string vector by reference since I will be modifying it. But, upon doing so I get the following error from the functions to pass it as an argument: error C2664: 'machineCorrections' : cannot convert parameter 2 from 'std::vector<_Ty>' to 'std::vector<_Ty> &' It seems the compiler has no problem doing this with the int vectors. Of course, the program is not yet finished. I am completing portions of its functionality then testing. At present, I'm attempting to overwrite the old error map with a new map that includes my corrected values. A sample of the type of file I'm working with is attached. Thanks for any help, -Brett Code: CPP / C++ / C Code:
|
|
#2
|
|||
|
|||
Re: changing values at specific elements in a vectorQuote:
It would be helpful if you would identify the specific lines that generate these errors. Compilers other than yours might generate other messages. (Furthermore, I'm guessing that you had other messages that might or might not be significant. Seeing these messages might help others to help you.) Why do I say that it might be helpful to us? Well, the reason is that the first thing is for you to look (yes, really look) at the error message, and then look at the line that is being flagged. I'll take a shot at the two that you posted. Quote:
Maybe this was flagging the following??? CPP / C++ / C Code:
Then MapData[i] is a string, and MapData[i][chosenColumn] is a char. On the other hand, holder is a string (I think). Now, does the error massage make sense? Now how about the other: Quote:
Maybe it is the following??? CPP / C++ / C Code:
What is argument 2? It's xMapData, A vector of vector of strings. (Why the heck is it a global variable????? But that's another issue.) What is parameter 2 of the function? It's a reference to a vector of strings. Now if the error message itself still doesn't mean much, just look at the way that you are calling the fucntion. There is no way that the compiler can make a reference to a vector of strings out of a vector of vector of strings. There are many, many other "issues" with your program. I can't see how or why you would put together over 1000 lines of source code without compiling parts of it as you are developing it, and apparently not understanding lots of fundamentals. As you make some corrections, you might notice that when you recompile the code, some other warnings or errors may go away and/or be replaced by something different. Look at the compiler messages (all of them, not just the obvious errors). Work your way through them one at a time or several at a time, but start logically at the top and work your way down. If you can't figure them out, then ask again. For example, what the heck are the following supposed to be doing: CPP / C++ / C Code:
Why do you have all of those global variables at the top of the program? Regards, Dave |
|
#3
|
|||
|
|||
Re: changing values at specific elements in a vectorAh, thanks for the response. Well I'm very much an amateur and am gradually learning C++ through books I have and of course, the net. Thus this code is a patchwork of the understanding I have thus far developed.
Prior to attempting to write the correction values in the function void machineCorrections back to the vector of vectors, I had confirmed that each previous function was working as I intended such that the capabilities of the program at that point was reading the text file, taking information from specific lines within it, taking in the necessary user input and working with this to generate corrections to the counts in the lines of the text from E20 to where ever it may end (differs from text file to text file). Here are all the errors my compiler generates: ------ Rebuild All started: Project: Error Map Utility, Configuration: Debug Win32 ------ Deleting intermediate and output files for project 'Error Map Utility', configuration 'Debug|Win32' Compiling... main.cpp c:\documents and settings\brett\my documents\visual studio 2005\projects\error map utility\error map utility\main.cpp(472) : warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data c:\documents and settings\brett\my documents\visual studio 2005\projects\error map utility\error map utility\main.cpp(473) : warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data c:\documents and settings\brett\my documents\visual studio 2005\projects\error map utility\error map utility\main.cpp(474) : warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data c:\documents and settings\brett\my documents\visual studio 2005\projects\error map utility\error map utility\main.cpp(67 with [ _Ty=std::vector<std::string> ] and [ _Ty=std::string ] c:\documents and settings\brett\my documents\visual studio 2005\projects\error map utility\error map utility\main.cpp(870) : error C2664: 'machineCorrections' : cannot convert parameter 2 from 'std::vector<_Ty>' to 'std::vector<_Ty> &' with [ _Ty=std::vector<std::string> ] and [ _Ty=std::string ] c:\documents and settings\brett\my documents\visual studio 2005\projects\error map utility\error map utility\main.cpp(1062) : error C2664: 'machineCorrections' : cannot convert parameter 2 from 'std::vector<_Ty>' to 'std::vector<_Ty> &' with [ _Ty=std::vector<std::string> ] and [ _Ty=std::string ] c:\documents and settings\brett\my documents\visual studio 2005\projects\error map utility\error map utility\main.cpp(1194) : warning C4244: 'argument' : conversion from 'double' to 'const int', possible loss of data c:\documents and settings\brett\my documents\visual studio 2005\projects\error map utility\error map utility\main.cpp(1200) : warning C4018: '<' : signed/unsigned mismatch c:\documents and settings\brett\my documents\visual studio 2005\projects\error map utility\error map utility\main.cpp(1301) : error C2440: '=' : cannot convert from 'std::string' to 'char' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called Build log was saved at "file://c:\Documents and Settings\Brett\My Documents\Visual Studio 2005\Projects\Error Map Utility\Error Map Utility\Debug\BuildLog.htm" Error Map Utility - 4 error(s), 5 warning(s) ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ========== In response to your questions: Quote:
Well MapData looks like this for example E20,0,0,0,0,0,0 E21,-32,-45,115,-10,45,41 MapData[i] referring to a row lets say, that E21 row. MapData[chosenColumn] would refer to a column within that row. Let's say it refers to column 3 which would be "115" (I ignored the commas when reading these lines). So that would look like literally: MapData[1][3] which produces the string "115". The row is a string and so I'd think the column elements would also be of type string, given that I haven't explicitly defined any vectors as type char. Quote:
Ok, I see what you're saying here. I had no idea I couldn't reference a vector of vectors. That poses a bit of a problem as I need to be able to change individual columns of said vector of vectors. Pardon the ignorance. As for the multitude of global variables, you pointing that out made me realize I hadn't looked back in this area for a while and there was some cleaning up to do there. Namely I was able to remove the vectors refering to x,y,z MapData and MapColumnData without adding more syntax errors. Fairly sure the removal won't cause any logic errors either. But anyways, some of them are used by more than one function and I found it easier to be able to call them globally. |
|
#4
|
|||
|
|||
Re: changing values at specific elements in a vectorQuote:
Compare the parameter types of the function with the types of variables in the argument list wherever you call the function. The problem is that one thing is a vector of vectors of strings and the other is a vector of strings. Quote:
Quote:
Sometimes there are very good reasons to use globals. Desire to type fewer function arguments or fewer local variable declarations is, in my opinion, not a very good reason. But that's just my opinion. You are entitled to your opinion. (I respectfully suggest that you not use such an argument if/when you ever submit your code to a design review in a professional context.) Regards, Dave Last edited by davekw7x : 18-Dec-2007 at 12:30.
|
Recent GIDBlog
Meeting the local Iraqis by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Vector Issues | Peter_APIIT | C++ Forum | 16 | 21-May-2007 07:40 |
| Combining Vectors and References | Frankg | C++ Forum | 7 | 14-Jan-2006 06:17 |
| Re: Changing Client Side Memory Values Using Tsearch | Zorachus | Computer Software Forum - Windows | 1 | 05-Aug-2005 21:03 |
| Trying to make a vector of structures, need help | CT++ | C++ Forum | 4 | 10-May-2005 16:00 |
| Pointer values changing unexpectedly | spudtheimpaler | C Programming Language | 11 | 04-Mar-2004 16:37 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The