[c++] Problem with std::string...

Started by
10 comments, last by silvermace 17 years, 1 month ago
Hey, first of all sorry about the poor discription of this topic but I would have no idea how else to describe this problem. My code is as follows:

// -- Header --
// Structure HeightMap
struct HEIGHTMAP { USINT heightData[100*100]; };



// -- Source Main--
// Create A HeightMapLoader Object
classMapLoader MapLoader;

// Create TestMap
HEIGHTMAP TestMap;

// Load TestMap From Heightmap
MapLoader.LoadMap("Data//HeightMaps//Test.raw", &TestMap);



// -- Source Class LoadMap --
void classMapLoader::LoadMap(std::string mapPath, HEIGHTMAP* mapArray)
{
  // Open mapPath And Load Data Into mapArray
  std::ifstream InputFileMap;

  InputFileMap.open( mapPath.c_str(), std::ios::in | std::ios::binary );

  if (InputFileMap.is_open())
  {
    InputFileMap.read((char*)(*mapArray).heightData, sizeof(USINT) * 100 * 100);

    InputFileMap.close();

    this->Logger->Notify("Heightmap Opened Correctly! File: " + mapPath);
  } else {
    this->Logger->Error("Could Not Open Heigtmap File! File: " + mapPath);
  }

  return;
}


Now if I debug this in the LoadMap function mapArray has the structure of an std::string. Meening when I look at mapArray in the Watch window I get a structure like this: - mapArray {...} + std::_String_val<char,std::allocator<char> > {...} + _Bx {...} _Myres 0 _Mysize 0 npos 4294967295 The interesting thing is that everything seems to be working fine when I switch the order of arguments in LoadMap like this:

void classMapLoader::LoadMap(HEIGHTMAP* mapArray, std::string mapPath);


Now I'm not sure why this is and any help would be greatly appreciated. If there is any request for more code I'll post it asap. Quinnie
Advertisement
thats kind of messed up,
what compiler + debugger are you using?
"I am a donut! Ask not how many tris/batch, but rather how many batches/frame!" -- Matthias Wloka & Richard Huddy, (GDC, DirectX 9 Performance)

http://www.silvermace.com/ -- My personal website
Sorry I forgot to mention that, I'm using Visual c++ 2003 .Net
Have you tried cleaning and rebuilding the project?

Are you certain that you don't have two copies of the header file lying around? Like, ANYWHERE on your hard disk?
Cleaning and rebuilding unfortunetly did not help, I'm also quite positive this is the only file with that name on my harddisk.

Also it might help to mention that with an other function I made I have the same problem, again switching the arguments seems to help.
Aside from the weird debugger information, what actually happens when you run the code?
Well, after the read function, where the array should be filled with data from the file, the std::string structure gets filled out with nonsense really. Also I can not acces the elements of mapArray (It is an unsigned short int array from 0 to 100000). When I switch the arguments as mentioned before the array gets filled out nicely and I can access the elements.
Are all of your source files correctly including the header file? I've seen compilers pass the first argument to a function OK, but any other ones get messed up if the function hasn't been prototyped. Just a thought.
Yeah, if the prototype has the arguments the wrong way around, it could cause this sort of problem. The compilation process may not spot the error since the total argument size is identical in both cases.
Quote:Original post by f8k8
Are all of your source files correctly including the header file? I've seen compilers pass the first argument to a function OK, but any other ones get messed up if the function hasn't been prototyped. Just a thought.


Any C++ compiler should prohibits this (and this is the case for VC++ 2003). A function has to be declared (or defined) before any use (like any other symbol).

This topic is closed to new replies.

Advertisement