0

I just came from Java and Python world to C++ world, and faced a problem while trying to get the value from a public const function of a class.

I have a class as follows:

class CMDPoint
{
public:
    CMDPoint();
    CMDPoint(int nDimensions);
    virtual ~CMDPoint();
private:
    int m_nDimensions;      // the number of dimensions of a point
    float* m_coordinate;    // the coordinate of a point
public:
    const int GetNDimensions() const { return m_nDimensions; }
    const float GetCoordinate(int nth) const { return m_coordinate[nth]; }
    void SetCoordinate(int nth, float value) { m_coordinate[nth] = value; }
};

Ultimately, I wish to write all the clusterPoints in clusterPointArray into the file. However, now I am just testing it with the first clusterPoint (thus, GetCoordinate(0)).

ofstream outFile;
outFile.open("C:\\data\\test.txt", std::ofstream::out | std::ofstream::app);
for (std::vector<CMDPoint> ::iterator it = clusterEntry->clusterPointArray.begin(); it   != clusterEntry->clusterPointArray.end(); ++it)
{
    outFile << ("%f", (*it).GetCoordinate(0)); // fails
    outFile << " ";
}
outFile << "\n";
outFile.close();

The problem is I only see the " " in the file. No coordinate has been written in. Did I do anything wrong while fetching the value from const float GetCoordinate(int nth)?

Sibbs Gambling
  • 19,274
  • 42
  • 103
  • 174
  • 3
    Just use `outFile << it->GetCoordinate(0);`. I also suggest adding bounds checking to `CMDPoint::GetCoordinate`, by using a `std::vector` instead of an array and subsequently using `m_coordinate.at(nth)`. Otherwise you'll probably run into segfaults if you are new to C++. – Marc Claesen Oct 09 '13 at 14:56
  • @MarcClaesen No, still the same. :/ – Sibbs Gambling Oct 09 '13 at 14:58
  • Have you ensured the coordinate index has been set correctly? i.e `m_coordinate[0]` actually has a value. You do no error checking. – Chemistpp Oct 09 '13 at 14:59
  • @Chemistpp even an uninitialized float would serialize to *some* number (or segfault if the array is a nullptr). – Marc Claesen Oct 09 '13 at 15:00
  • @Chemistpp I also tried GetCoordinate(1) still the same. Do you mean by this? – Sibbs Gambling Oct 09 '13 at 15:00
  • What is this? I've never seen it before. `outFile << ("%f", (*it).GetCoordinate(0)); // fails` – Neil Kirk Oct 09 '13 at 15:00
  • I seriously wonder where you get from that `outFile << ("%f", (*it).GetCoordinate(0));` will do remotely waht you want in c++... – PlasmaHH Oct 09 '13 at 15:01
  • Where do you allocate space for the array? – Beta Oct 09 '13 at 15:01
  • 3
    You don't need to return `const int` and `const float`. It's superfluous. You don't need to force your user to handle them as const. – Stephane Rolland Oct 09 '13 at 15:01
  • What does "fails" even mean? Doesn't compile? Crash? Do the wrong thing? – Sebastian Redl Oct 09 '13 at 15:03
  • @perfectionm1ng Well, have you tried just a standard `std::cout << m_coordinate[nth];` in your get function to ensure the value is correctly stored in the array? However, Marc Claesen suggest this wouldn't be the problem cause, and he's probably right. You'd throw an uninitialized variable usage if using visual studio or it would have some value or seg fault. – Chemistpp Oct 09 '13 at 15:04
  • @SebastianRedl compiles but does nothing – Sibbs Gambling Oct 09 '13 at 15:04
  • 2
    I think clusterEntry->clusterPointArray has no elements. You can check with the debugger is code actually goes inside the for loop. – Raxvan Oct 09 '13 at 15:06

2 Answers2

2

try to change this

outFile << ("%f", (*it).GetCoordinate(0)); // fails

to this:

outFile << (*it).GetCoordinate(0); // OK

Because the ("%f", (*it).GetCoordinate(0)) represents nothing , only a enumerations of expressions separated by , . It will not be evaluated into a pair of objects as in java is i think.

Edit:("%f", (*it).GetCoordinate(0)) actually evaluates to the last element that is (*it).GetCoordinate(0) ( PlasmaHH comment ) so it should still print something. However if nothing is printed then the collection clusterEntry->clusterPointArray could be empty and the code inside the for loop might not be executed ever.

Hope this helps, Razvan.

Raxvan
  • 6,257
  • 2
  • 25
  • 46
  • 1
    It will actually evalaute to the last one, just like `std::cout << (1,2,3)` will print out `3` – PlasmaHH Oct 09 '13 at 15:02
  • PlasmaHH 4 you were right about that, i was wondering how is that compiling , i have never used that. – Raxvan Oct 09 '13 at 15:09
0
outFile << it->GetCoordinate(0);
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91