I am working off the code that the professor provided, but for some reason I cannot get it to work. It is giving me errors.

I created a project in Visual Studio.

The header file:

#include <iostream>
#include <math.h>
using namespace std;

class xy_coordinate
{
public:
    void input();
    void print();
    double radius();
    double angle();
private:
    double x,y;
};

Source:

#include <iostream>
#include <math.h>
using namespace std;

void xy_coordinate::input()

{cin>>x>>y;}

void xy_coordinate::print ()

{cout << "(" << x << "," << y <<")" << "\n";}

double xy_coordinate::radius()
{return sqrt (x*x+y*y);}

double xy_coordinate::angle()
{double z, pi= 3.141593;
if (x>=0)
z= atan (y/x);
if (x<0 && y>0)
z = atan (y/x) + pi;
if (x<0 && y <=0)
z = atan (y/x) - pi;
if (x == 0 && y ==0)
z= 0;
return z;

}

I had to replace #include <iostream.h> with #include <iostream>
and adding using namespace std;

because visual studio would not find iostream.h

the error I recieve are the following:

1>source.cpp(5) : error C2653: 'xy_coordinate' : is not a class or namespace name
1>source.cpp(7) : error C2065: 'x' : undeclared identifier
1>source.cpp(7) : error C2065: 'y' : undeclared identifier
1>source.cpp(9) : error C2653: 'xy_coordinate' : is not a class or namespace name
1>source.cpp(13) : error C2653: 'xy_coordinate' : is not a class or namespace name
1>source.cpp(16) : error C2653: 'xy_coordinate' : is not a class or namespace name

Recommended Answers

All 15 Replies

Member Avatar for iamthwee

i think something is wrong in your code. Have you debugged it.

Yes, I have debugged.

Makes me think that a part of the code is missing and that the source code actually goes inside the header.

But then where is the source. O_O

Hopefully I am wrong in this and there is a mistake on what I have provided. :D

obviously. For starters he's missing an include for his very own headerfile...
I didn't look further than that.

Member Avatar for iamthwee

Have you compiled the source?

ok, I placed this in the source file:

#include "xy_coordinate.h"

now i get 6 errors:

1>source.cpp(8) : error C2084: function 'void xy_coordinate::input(void)' already has a body

1>xy_coordinate.h(8) : see previous definition of 'input'

1>source.cpp(12) : error C2084: function 'void xy_coordinate::print(void)' already has a body

1> xy_coordinate.h(9) : see previous definition of 'print'

1>source.cpp(15) : error C2084: function 'double xy_coordinate::radius(void)' already has a body

1> xy_coordinate.h(10) : see previous definition of 'radius'

1>source.cpp(18) : error C2084: function 'double xy_coordinate::angle(void)' already has a body

1>xy_coordinate.h(11) : see previous definition of 'angle'

Member Avatar for iamthwee

Does your source have a main?

I just placed it, but it showed me more errors.

int main (void){
...
}

Member Avatar for iamthwee

Is your header file linking to main prog?

doesn't the #include "xy_coordinate" link the file? and I am compiling the whole project together...

Member Avatar for iamthwee

>doesn't the #include "xy_coordinate" link the file?

let's say you have several hundred drives, and you put the #include xy_coordinate" in say drive number 147. Do you think visual studio will be able to find it?

yes?

since it is all in a folder created automatically by visual studio.

The header file is under the header subsection of the project.

If i am incorrect, can you show me how to link the files together

Member Avatar for iamthwee

oh i don't use visual studio or c++. Sorry.

I am working off the code that the professor provided, but for some reason I cannot get it to work. It is giving me errors.

---

the error I recieve are the following:

1>source.cpp(5) : error C2653: 'xy_coordinate' : is not a class or namespace name
1>source.cpp(7) : error C2065: 'x' : undeclared identifier
1>source.cpp(7) : error C2065: 'y' : undeclared identifier
1>source.cpp(9) : error C2653: 'xy_coordinate' : is not a class or namespace name
...

Sorry, but this thread had me shaking my head in amazement!

Dumb help #1:

i think something is wrong in your code. Have you debugged it.

Duh, you think something's wrong? What's your first clue?
And how do you debug something that doesn't even compile? :icon_rolleyes:

Dumb help #2:

Have you compiled the source?

Where do you think the errors come from? :icon_rolleyes:


ok, I placed this in the source file:

#include "xy_coordinate.h"

now i get 6 errors:

1>source.cpp(8) : error C2084: function 'void xy_coordinate::input(void)' already has a body
...
1>xy_coordinate.h(8) : see previous definition of 'input'

1>source.cpp(12) : error C2084: function 'void xy_coordinate::print(void)' already has a body

1> xy_coordinate.h(9) : see previous definition of 'print'

1>source.cpp(15) : error C2084: function 'double xy_coordinate::radius(void)' already has a body
...

Dumb help #3:

Is your header file linking to main prog?

With the change in errors, isn't it obvious the header is linking in? It's just linking wrong.
Note 'void xy_coordinate::input(void)' already has a body
It can't have a body if the header isn't found...


Dumb help #4:

>doesn't the #include "xy_coordinate" link the file?

let's say you have several hundred drives, and you put the #include xy_coordinate" in say drive number 147. Do you think visual studio will be able to find it?

Yeah, that's a likely problem. :icon_rolleyes:

tformed, since you made changes, repost the code and the errors...

commented: b i n g o! +12

The changes made thus far and errors:

Source.cpp :

#include <iostream>
#include <math.h>
#include "xy_coordinate.h"
using namespace std;

void xy_coordinate::input()

{cin>>x>>y;}

void xy_coordinate::print ()

{cout << "(" << x << "," << y <<")" << "\n";}

double xy_coordinate::radius()
{return sqrt (x*x+y*y);}

double xy_coordinate::angle()
{double z, pi= 3.141593;
if (x>=0)
z= atan (y/x);
if (x<0 && y>0)
z = atan (y/x) + pi;
if (x<0 && y <=0)
z = atan (y/x) - pi;
if (x == 0 && y ==0)
z= 0;
return z;

}

xy_coordinate.h :

#include <iostream>
#include <math.h>
using namespace std;

class xy_coordinate
{
public:
    void input();
    void print();
    double radius();
    double angle();
private:
    double x,y;
};
void xy_coordinate::input()

{cin>>x>>y;}

void xy_coordinate::print ()

{cout << "(" << x << "," << y <<")" << "\n";}

double xy_coordinate::radius()
{return sqrt (x*x+y*y);}

double xy_coordinate::angle()
{double z, pi= 3.141593;
if (x>=0)
z= atan (y/x);
if (x<0 && y>0)
z = atan (y/x) + pi;
if (x<0 && y <=0)
z = atan (y/x) - pi;
if (x == 0 && y ==0)
z= 0;
return z;

}

Errors:

1> source.cpp(8) : error C2084: function 'void xy_coordinate::input(void)' already has a body
1> xy_coordinate.h(8) : see previous definition of 'input'
1> source.cpp(12) : error C2084: function 'void xy_coordinate::print(void)' already has a body
1> xy_coordinate.h(9) : see previous definition of 'print'
1> source.cpp(15) : error C2084: function 'double xy_coordinate::radius(void)' already has a body
1> xy_coordinate.h(10) : see previous definition of 'radius'
1> source.cpp(18) : error C2084: function 'double xy_coordinate::angle(void)' already has a body
1> xy_coordinate.h(11) : see previous definition of 'angle'

Implementation of instance methods should occur only once -- either in the header file or the cpp file. The error message say it all : you are providing a definition of your instance methods in both the header file as well as the cpp file.

The structure of a normal project is:
• MyClass.h (the class declaration, containing the function prototypes)
• MyClass.cpp (the class definition, containing the implementation)
• Driver.cpp (the program which uses MyClass and other classes, normally the entry point of program)

So just remove the function definition from the header files keeping only the declaration. Also it doesn't make sense to keep a header file if all you are going to do is implement the methods there itself.

And BTW, its #include <cmath> and not #include <math.h>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.