Migrating from C, want to implement class
I'm trying to update a C program written 20+ years ago. I want to use current compilers and standards. I'm looking at this as a good learning process, beyond reading C++ programming guides and reading code that has no real-world applications. I've already updated all the function headers to current ANSI standard headers. I'm now trying to write a class with functions.
The existing code is:
Code:
typedef struct xycoord
{
int x, y;
} coord;
My new code is:
Code:
class coord
{
public:
int x, y;
coord(int xin, int yin) { x = xin; y = yin; }
coord();
void operator=(coord &rhs);
coord operator+(coord &other);
bool operator==(coord &other);
};
void coord::operator=(coord &rhs)
{
x = rhs.x;
y = rhs.y;
}
coord coord::operator+(coord &other)
{
return coord(x + other.x, y + other.y);
}
bool coord::operator==(coord &other)
{
return (x == other.x && y == other.y);
};
I know the new code I've written will compile when I put it in a fresh C++ project. However, when I try to compile it in the old program, I get thousands of errors.
Compiling...
makedefs.c
c:\...\coord.h(20) : error C2061: syntax error : identifier 'coord'
c:\...\coord.h(20) : error C2059: syntax error : ';'
c:\...\coord.h(21) : error C2449: found '{' at file scope (missing function header?)
c:\...\coord.h(31) : error C2059: syntax error : '}'
c:\...\coord.h(39) : error C2061: syntax error : identifier 'coord'
c:\...\coord.h(39) : error C2059: syntax error : ';'
...
I'm sure I'm missing something fundamental. Please help. I don't know where to look.