C++ Headers and Scope Error

Recommended Videos

WinterOrbit

New member
Aug 5, 2009
114
0
0
Hello Escapists,

I'm learning C++ at the moment and writing a basic program to get into the syntax. However, I can't seem to get my method definitions and declarations right, and at this point, I'm getting "was not declared in this scope" errors. Would anyone mind telling me what I'm missing here?

The code:

Program1Math.h

#ifndef PROGRAM1MATH_H_
#define PROGRAM1MATH_H_
#include
#include <math.h>
using namespace std;

class Program1Math {
void calculateSine(int a);
void calculateTangent(int a);
void calculateModulus(int a, int b);
};

#endif /* PROGRAM1MATH_H_ */




Program1Math.cpp

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

void calculateSine(int a){
cout<<"Sine("<<a<<") ="<<sin(a);
}

void calculateTangent(int a){
cout<<"Tan("<<a<<") ="<<tan(a);
}

void calculateModulus(int a, int b){
if (a%b==0){
cout<<"/n"<<a<<" is a multiple of "<<b<<"!";
}
else{
cout<<"\n"<<a<<" is not a multiple of "<<b<<".";
}
}



Program1.cpp

#include "Program1Math.h"
#include
using namespace std;

int main(){

int num1;
int num2;
int i=1;

/*Note: I mean this to loop infinitely here.
while (i>0){
cout<<"Please enter the first integer number:\n";
cin>>num1;
cout<<"\nPlease enter the second integer number:\n";
cin>>num2;

calculateModulus(num1, num2);
calculateSine(num1);
calculateTangent(num1);
calculateSine(num2);
calculateTangent(num2);

cout<<"/n/n";
}
return 0;
}
 

SturmDolch

This Title is Ironic
May 17, 2009
2,341
0
0
I can't really see it... Are you adding the -PROGRAM1MATH_H tag to the compiler? Does the compiler give you a line number where the error is occuring?

Try using GDB [http://www.gnu.org/software/gdb/] to debug your program and go through it step by step.
 

WinterOrbit

New member
Aug 5, 2009
114
0
0
Sturmdolch said:
I can't really see it... Are you adding the -PROGRAM1MATH_H tag to the compiler? Does the compiler give you a line number where the error is occuring?

Try using GDB [http://www.gnu.org/software/gdb/] to debug your program and go through it step by step.
The compiler I'm using (Eclipse) gave this error message:

**** Build of configuration Debug for project Program1Math ****

**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -oProgram1.o ..\Program1.cpp
..\Program1.cpp: In function 'int main()':
..\Program1.cpp:24: error: 'calculateModulus' was not declared in this scope
..\Program1.cpp:25: error: 'calculateSine' was not declared in this scope
..\Program1.cpp:26: error: 'calculateTangent' was not declared in this scope
Build error occurred, build is stopped
Time consumed: 406 ms.

I haven't tried debugging with GDB yet. I don't know if I would need to make my own makefile and how I'd go about doing that or if it's another matter entirely.
 

Tharwen

Ep. VI: Return of the turret
May 7, 2009
9,144
0
41
Dammit, C++ is too hard.

Well, it seems Program1Math.h isn't being included properly. Are you sure those functions need to be inside their own Program1Math class? I'd suggest trying them on their own in the namespace.
 

Snork Maiden

Snork snork
Nov 25, 2009
1,071
0
0
Builds just fine for me if you take the function prototypes (ie. void calculateSine(int a) etc) out of the class brackets. Actually I just removed that bit altogether.

So you go from:

class Program1Math {
void calculateSine(int a);
void calculateTangent(int a);
void calculateModulus(int a, int b);
};
To:

void calculateSine(int a);
void calculateTangent(int a);
void calculateModulus(int a, int b);
Note that I know diddly about C++ - as a language I don't get along with it and haven't used it for a couple of years, but after messing with the compiler I got it to at least compile by doing this.

Incidentally, if you're ever so stuck you want to come online to ask programming questions, I cannot recommend StackOverflow [http://stackoverflow.com/] enough - even if you don't want to sign up, I find there's a 90% chance someone else will of asked a similar question before which you can search on. It's like a million times better than DaniWeb, but unfortunately Google doesn't pick it up all the time.
 

burningdragoon

Warrior without Weapons
Jul 27, 2009
1,934
0
0
ok, first of all, use code tags. like so:
Code:
code stuff here
becomes
Code:
code stuff here
anyway the problem I believe is like Snork Maiden said.


however to explain what (I think/pretty sure) is the reasoning is.

in the .h file you have the class definition:

Code:
class Program1Math {
   void calculateSine(int a);
   void calculateTangent(int a);
   void calculateModulus(int a, int b);
};
Except you never actually use the class. So, you are trying to call methods connected to a class without saying what class or declaring a new class reference... (or w/e not important atm)

if you remove the class definition from the .h file, when you do
Code:
#include "Program1Math.h"
you are including all of the methods directly into the .cpp file.

Let me know if that made enough sense. I (mostly) know what I am talking about, but am not all that great at explaining it.

edit: wow... this is about 12 24 hours later, you've probably figured it out by now hehe
 

Snork Maiden

Snork snork
Nov 25, 2009
1,071
0
0
burningdragoon said:
however to explain what (I think/pretty sure) is the reasoning is.
Glad you could give a proper explanation. I could've tried but, well, it probably would've been wrong.

Also I'm sure I tested to see if code tags existed, but I guess my test wasn't very rigorous because clearly they do :p