I'm using visual basic 2010. I have tried everything I can think of to eliminate these errors. It's a simple idea behind the program but I'm curious if the issue has something to do with how I'm referencing or calling my variables. I an new to C++, any help or suggestions would be greatly received. Thanks in advance.
Box.cpp
#include "stdafx.h"
#include "Box.h"
#include <iostream>
#include <iomanip>
using namespace std;
double getVolume(int A, int B, int C){
double totalVolume;
totalVolume = A * B * C;
return totalVolume;
}
double getSurfaceArea(int A, int B, int C){
double totalSurface;
totalSurface = (A*B*2) + (B*C*2) + (A*C*2);
return totalSurface;
}
bool perfectBox(int A, int B, int C){
bool perfect;
if (A = B)
if (B = C)
perfect = true;
else
perfect = false;
else
perfect = false;
return perfect;
}
//Box.h
class Box
{
public:
int A, B, C;
Box(int A, int B, int C);
double getVolume(int A, int B, int C);
// get volume of entered sides
double getSurfaceArea(int A, int B, int C);
// calculate surgace are based on sides
bool perfectBox(int A, int B, int);
// compare all 3 sides to determine if box is perfect
};
//Main.cpp
#include "stdafx.h"
#include "Box.h"
#include <iostream>
using namespace std;
Box::Box(int a, int b, int c){
}
int main()
{
int a, b, c;
cout << "Enter 3 side lengths of a box to determine volume, surface area and if it's perfect...
\n";
cout << "length of Side 1: ";
cin >> a;
cout << endl << "Side 2: ";
cin >> b;
cout << endl << "Side 3: ";
cin >> c;
Box test(a, b, c);
cout << "Total Area: " << test.getVolume(a, b, c) << endl;
cout << "Total Surface: " << test.getSurfaceArea(a, b, c) << endl;
cout << "Is it a perfect box: " << test.perfectBox(a, b, c) << endl;
system ("Pause");
return 0;
}
You miss your namespace declaration. When you refer to getVolume, it's not just getVolume, but Box::getVolume.
double Box::getVolume(int A, int B, int C){
double totalVolume;
totalVolume = A * B * C;
return totalVolume;
}
double Box::getSurfaceArea(int A, int B, int C){
double totalSurface;
totalSurface = (A*B*2) + (B*C*2) + (A*C*2);
return totalSurface;
}
bool Box::perfectBox(int A, int B, int C){
bool perfect;
if (A = B)
if (B = C)
perfect = true;
else
perfect = false;
else
perfect = false;
return perfect;
}
Can you please show the compiler output?
In your constructor you haven't assigned A, B, C so perhaps there is an issue there. Also you are using assignment operators i.e. =, not comparison operator i.e. == which are two different commands.
Simple rule: Every Function of class you want to use, must be declared [float functioName(int param)] in the .h file of the class. This includes the constructor classname(int a);
There is a missing constructor declaration in box.h
Box(int a, int b, int c);
should be move in box.cpp
Box::Box(int a, int b, int c)
{
}
Tip from a researcher of our computer science deparment:
first include system header then local headers:
#include <iostream>
#include <iomanip>
#include "stdafx.h"
#include "Box.h"
instead of (different order depending on <> or ""):
#include "stdafx.h"
#include "Box.h"
#include <iostream>
#include <iomanip>
There also missing functions in the .h File. Every function which is defined in box.cpp must be declared in box.h
Declare:
float getVolume();
Define:
float Box::getVolume()
{
float localVariableForVolume = A*B*C;
//this works too float localVariableForVolume = this->A*this->B*this->C;
return(localVariableForVolume);
}
Related
What I want to do (c++ problem):
Enter main. Call Class A (and pass a value). Inside class A, I call Class B (and pass a value). Do some stuff in class B. Return value back to Class A. Do some more stuff in A. Return back to main function.
I get the error that obj3 is an unknown override specifier. I tried to create a simple program to showcase my problem;
#include <math.h>
#include <string>
#include <stdio.h>
#include <iomanip>
#include <iostream>
using namespace std;
class A
{
B obj3;
public:
int add3(int num)
{
int x = num + 1;
int y = obj3.add2(x);
return y;
}
};
class B
{
public:
int add2(int num2)
{
int y = num2 + 2;
return y;
}
};
int main()
{
int g;
A obj1;
cout << "enter a number: " << endl;
cin >> g;
int r = obj1.add3(g);
cout << r;
system("pause");
return 0;
}
I have been trying to code a program that can solve for c using the Law Of Cosines. The program runs correctly, but the answer I get is ridiculously big, noted by how it was in scientific notation.
Here is my code:
#include <iostream>
#include <cmath>
using namespace std;
class TrigMath
{
private:
double a;
double b;
double y;
public:
double LawOfCos()
{
return sqrt(pow(a,2) + pow(b,2) - 2*a*b*cos(y));
}
void seta(double A)
{
A = a;
}
void setb(double B)
{
B = b;
}
void sety(double Y)
{
Y = y;
}
};
int main()
{
TrigMath triangle1;
triangle1.seta(3);
triangle1.setb(4);
triangle1.sety(60);
cout << "c is equal to " << triangle1.LawOfCos() << endl;
return 0;
}
The cos() function there takes input as radians not as degrees.
Try to convert degrees to radians and then supply it as input.
In the class functions seta, setb and sety you have written A = a, B = b and Y = y.
You have to change them to a = A, b = B and Y = y.
So after applying all the changs the code should be like
#include <iostream>
#include <cmath>
using namespace std;
class TrigMath
{
private:
double a = 0;
double b = 0;
double y = 0;
public:
double LawOfCos()
{
return sqrt(pow(a,2) + pow(b,2) - 2*a*b*cos(y));
}
void seta(double A)
{
a = A;
}
void setb(double B)
{
b = B;
}
void sety(double Y)
{
y = Y*3.14/180;
}
};
int main()
{
TrigMath triangle1;
triangle1.seta(3.0);
triangle1.setb(4.0);
triangle1.sety(60.0);
cout << "c is equal to " << triangle1.LawOfCos() << endl;
return 0;
}
I am working on a project for a class and I am having some troubles, that gave me 2 errors which i don't understand what they mean...
It gave the error: c4716 "medie" must return a value.
Here is the code:
#include <iostream>
#include <stdlib.h>
#include<math.h>
using namespace std;
float medie(float a, float b, float c)
{
float MG,MA;
MG= sqrt(a*b*c);
cout<< "MG="<< MG<<endl;
MA=(2*a*b*c)/(a+b+c);
cout<< "MA="<< MA<<endl;
}
float medie(float a,float b,float c,float d)
{
float MG,MA;
MG= sqrt(a*b*c*d);
cout<< "MG="<< MG<<endl;
MA=(2*a*b*c*d)/(a+b+c+d);
cout<< "MA="<< MA<<endl;
}
int main()
{
float a,b,c,d;
cout<<"a="<<endl;
cin>>a;
cout<<"b="<<endl;
cin>>b;
cout<<"c="<<endl;
cin>>c;
cout<<"d="<<endl;
cin>>d;
medie(a,b,c);
medie(a,b,c,d);
}
Your medie function is declared to return a float value, but you don't have any return statement in it. If you declare them to return void the error should go away.
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
void medie(float a, float b, float c)
{
float MG,MA;
MG = sqrt(a*b*c);
cout<< "MG="<< MG<<endl;
MA = (2*a*b*c)/(a+b+c);
cout<< "MA="<< MA<<endl;
}
void medie(float a,float b,float c,float d)
{
float MG,MA;
MG = sqrt(a*b*c*d);
cout<< "MG="<< MG<<endl;
MA = (2*a*b*c*d)/(a+b+c+d);
cout<< "MA="<< MA<<endl;
}
int main()
{
float a,b,c,d;
cout<<"a="<<endl;
cin>>a;
cout<<"b="<<endl;
cin>>b;
cout<<"c="<<endl;
cin>>c;
cout<<"d="<<endl;
cin>>d;
medie(a,b,c);
medie(a,b,c,d);
}
I'm new to programming, I was trying this program to copy one string into another string but it's showing error
"error C3861: 'copyString': identifier not found"
Here is the code that I wrote
#include <iostream>
using namespace std;
int main()
{
char a[8], b[8];
cout << "enter the string a";
cin.get(a, 8);
cout << a;
int len = sizeof(a) / sizeof(char);
copyString(a, b);
int i;
cin >> i;
return 0;
}
/*function that copy one string to another*/
void copyString(char* a, char* b)
{
int i = 0;
while (a[i] != '\0') {
b[i] = a[i];
i++;
}
cout << b << " String is this";
}
Please tell me where am I mistaking??
Either provide copyString implementation before main, or provide a prototype for it first:
void copyString(char *a,char *b); // prototype of copyString
int main()
{
...
}
void copyString(char *a,char *b) // implementation of copyString
{
...
}
C++ compiler requires you to provide the declarations before a function or a variable is used. To solve your problem, you can simply place a forward declaration of copyString() before main().
#include <iostream>
using namespace std;
void copyString(char* a, char* b); // this is the forward declaration
int main()
{
char a[8], b[8];
cout << "enter the string a";
cin.get(a, 8);
cout << a;
int len = sizeof(a) / sizeof(char);
copyString(a, b);
int i;
cin >> i;
return 0;
}
/*function that copy one string to another*/
void copyString(char* a, char* b)
{
/* here is the real implementations */
}
However, when your program growth and more and more functions (with inter-dependence) are added, I would recommend you to split those functions into a separated header file and a source for easier maintenance.
main.cpp
#include <iostream>
using namespace std;
#include "my_string_lib.h" // PLEASE notice this line
int main()
{
char a[8], b[8];
cout << "enter the string a";
cin.get(a, 8);
cout << a;
int len = sizeof(a) / sizeof(char);
copyString(a, b); // included in my_string_lib.h
int i;
cin >> i;
return 0;
}
my_string_lib.h
#pragma once // assume you are using msvc
/*!
Copy the string content from a to b,
assume b is allocated large enough to hold a.
*/
void copyString(char* a, char* b);
my_string_lib.cpp
#include "my_string_lib.h"
void copyString(char* a, char* b)
{
/* here is the real implementations for copying a to b*/
}
Please make sure that main.cpp, my_string_lib.cpp and my_string_lib.h are placed inside the same directory.
I'm trying to create a small program for a lessons.
But In My class that herit , I don't have values of the superclass.
, here's the code
Point.h
#ifndef Point_H
#define Point_H
#include <iostream>
class Point{
public:
Point();
void set_values (int a, int b);
void set_values (int a, int b, int c);
void affichervaleurs();
protected:
int x ;
int y ;
int z ;
};
#endif
Point.cpp
#include <iostream>
#include "Point.h"
using namespace std;
Point::Point(){
x=0;
y=0;
z=0;
};
void Point::set_values (int a, int b){
x=a;
y=b;
}
void Point::set_values (int a = 0, int b = 0, int c = 0){
x=a;
y=b;
z=c;
}
void Point::affichervaleurs(){
cout << "X = " << x << endl;
cout << "Y = " << y << endl;
}
Carre.h
#ifndef Carre_H
#define Carre_H
#include "Point.h"
class Carre:public Point{
public:
int Aire (){
};
void affichercar(){
};
};
#endif
Carre.cpp
#include <iostream>
using namespace std;
#include "Point.h"
class Carre:public Point{
public:
//Carre::Carre(int a, int b);
int Aire (){
return (x * y);
}
void affichercar(){
cout << "Coordonnees X:" << x << endl;
}
};
main.cpp
#include <cstdlib>
#include <iostream>
#include "Carre.h"
#include "Point.h"
using namespace std;
int main()
{
Point MonPoint ;
cout << "Default values:" << endl;
MonPoint.affichervaleurs();
MonPoint.set_values(4,6);
cout << "Setting values:" << endl;
MonPoint.affichervaleurs();
Carre MonCarre;
MonCarre.set_values(4,6,0);
MonCarre.set_values(5,8);
MonCarre.affichercar();
cout << MonCarre.Aire() << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
The problem is, when I call this line :
cout << MonCarre.Aire() << endl;
it returns a strange number. I'm sure that the problem is here :
int Aire (){
return (x * y);
};
Like the x and y are not linked with the superclass.
Can someone help me how to access my x and y values from my superclass in my class Carre ?
Other strange thing this line doesn't appear :
MonCarre.affichercar();
Thanks
You define the functions Aire and affichercar in Carre.h, but you need to declare them only if you want to write an implementation in Carre.cpp.
Edit: and your Carre.cpp is also wrong. You just need to rewrite Carre.* files.
Edit2. Let me do some code review and give you a working version of your code - just summarizing what was discussed in comments and my own thoughts.
Point.h
void set_values (int a, int b);
void set_values (int a, int b, int c);
If you want to define the default values of the arguments of the second function, you should do it in its declaration as was suggested in comments to your question. So it will be
void set_values (int a, int b);
void set_values (int a = 0, int b = 0, int c = 0);
But in this case it's not clear what function should be called if you write, for example, set_values(1, 2), therefore your code won't compile because of ambiguity (as also was mentioned in the comments). So you just have to keep only one extended version of this function with the default arguments. The Point.h will be in this case:
#ifndef Point_H
#define Point_H
#include <iostream>
class Point{
public:
Point();
void set_values (int a = 0, int b = 0, int c = 0);
void affichervaleurs();
protected:
int x ;
int y ;
int z ;
};
#endif
The Point.cpp is also changed a little bit:
#include <iostream>
#include "Point.h"
using namespace std;
Point::Point()
// it's better to initialize these variables here
// : x(0), y(0), z(0)
{
x=0;
y=0;
z=0;
}
void Point::set_values (int a, int b, int c){
x=a;
y=b;
z=c;
}
void Point::affichervaleurs(){
cout << "X = " << x << endl;
cout << "Y = " << y << endl;
}
Now let's consider the Carre.h file
class Carre:public Point{
public:
// IT'S DEFINITION WHICH IS USED WHEN YOU CALL THE FUNCTION
int Aire (){};
// IT'S DEFINITION WHICH IS USED WHEN YOU CALL THE FUNCTION
void affichercar(){};
};
Now I'm starting answering your questions:
The problem is, when I call this line :
cout << MonCarre.Aire() << endl;
it returns a strange number.
Yes, it's a strange number returned by the function defined in the Carre.h (not Carre.cpp) file.
I'm sure that the problem is here :
int Aire (){
return (x * y);
};
No, as you see, it's not.
Like the x and y are not linked with the superclass.
In fact they are.
Can someone help me how to access my x and y values from my superclass in my class Carre ?
You already have this access. It's okay to use protected members from base class in derived class in case of public inheritance.
Other strange thing this line doesn't appear : MonCarre.affichercar();
You mean it does nothing? But it's in perfect consistency with how you implement (define) the function in Carre.h (not Carre.cpp) file.
So let me change the code to make it work.
Carre.h
#ifndef Carre_H
#define Carre_H
#include "Point.h"
class Carre:public Point{
public:
int Aire (); // now it's declaration
void affichercar(); // now it's declaration
};
#endif
Carre.cpp
#include <iostream>
using namespace std;
int Carre::Aire (){
return (x * y);
}
void Carre::affichercar(){
cout << "Coordonnees X:" << x << endl;
}
I didn't compile this for myself, but the whole idea should be clear.
access my x and y values
From within Carre, you simply access them by 'x' and 'y'.
From outside, you cannot as they are not public.