C++ - Error C2511: overloaded member function not found in 'BMI' - c++

Got an error on my C++ program. It is likely to be something simple as I have only just started programming.
The error is:
Error 1 error C2511: 'void BMI::getWeight(double)' : overloaded member function not found in 'BMI' c:\users\**********\documents\visual studio 2012\projects\project2\project2\bmi.cpp 40 1 Project2
bmi.h:
#include <iostream>
#include <string>
using namespace std;
#ifndef BMI_H
#define BMI_H
class BMI {
public:
//Defualt Constructor
BMI();
//Overload Constructor
BMI(string, int, double);
//Destructor
~BMI();
//Accessor Functions
string getName() const;
// getName - returns name of paitent
int getHeight() const;
//getHeight - returns height of paitent
double getWeight() const;
//getWeight returns weight of paitent
private:
//Member Variables
string newName;
int newHeight;
double newWeight;
};
#endif
bmi.cpp:
// Function Definitions
#include "BMI.h"
BMI::BMI() {
newHeight = 0;
newWeight = 0.0;
}
BMI::BMI(string name, int height, double weight) {
newName = name;
newHeight = height;
newWeight = weight;
}
BMI::~BMI() {
}
string BMI::getName() const {
return newName;
}
int BMI::getHeight() const {
return newHeight;
}
double BMI::getWeight() const {
return newWeight;
}
void BMI::setName(string name) {
newName = name;
}
void BMI::setHeight(int height) {
newHeight = height;
}
void BMI::setWeight(double weight) {
newWeight = weight;
}

Ok, when I try to compile the code I see a couple of problems:
The setName(string) function in the .cpp doesn't match anything in the header.
The setHeight(int) function in the .cpp doesn't match anything in the header.
The setWeight(double) function in the .cpp doesn't match anything in the header.
I would try to solve the compilation errors in the order they occur, and then see if you still have a problem with getWeight. I'm assuming that you are seeing the same problems with the undeclared functions that I'm seeing.

The error appears to be telling you that you are trying to call BMI::getWeight() somewhere and you are passing in it a parameter with a double type. This error is a bit perplexing as no such function that matches void BMI::getWeight(double) defined in either the BMI class in the header file or the cpp file. If you have changed the code since you posted it up then please do update and post ALL of the compiler messages. I suspect that you have not posted all of the compiler messages because SetName,setHeight and setWeight are all missing from the BMI class definition. So make sure you add all of those into the BMI class.
Also I think that it's good practice to initialize your data members differently. So instead of:
BMI::BMI(string name, int height, double weight) {
newName = name;
newHeight = height;
newWeight = weight;
}
you should prefer:
BMI::BMI(string name, int height, double weight):
newName(name),
newHeight(height),
newWeight(weight)
{ }

Related

C++ error C2084: function already has a body

I know this question has been asked before, but it's a clear issue in every other case. Everyone accidentally called their constructor twice. I, on the other hand, am having this issue because of prototypes in a header file, and it makes no damn sense. I'm having the error called on every single function called between these two files. Thanks!
Auto.h
#ifndef AUTO_H
#define AUTO_H
#include<string>
using std::string;
class Auto
{
public:
Auto();
Auto(const char* mk, const char* ml, int d);
void setDoors(int d);
int getDoors(void) const;
const string getMake(void) const;
const string getModel(void) const;
void setMake(const char *mk);
void setModel(const char *ml);
private:
int doors;
string make;
string model;
};
#endif
Auto.cpp
#include "Auto.h"
Auto::Auto()
{
// The strings are constructed empty by their default construtors
doors = 2;
}
Auto::Auto(const char* mk, const char* ml, int d)
{
setMake(mk);
setModel(ml);
setDoors(d);
return;
}
void Auto::setDoors(int d)
{
if (d>0)
doors = d;
else
doors = 2;
return;
}
int Auto::getDoors(void) const
{
return doors;
}
const string Auto::getMake(void) const
{
return make;
}
const string Auto::getModel(void) const
{
return model;
}
void Auto::setMake(const char *mk)
{
if (mk != 0) {
make = mk;
}
return;
}
void Auto::setModel(const char *ml)
{
if (ml != 0) {
model = ml;
}
return;
}
Error messages:
1> Lab11.cpp 1>m:\cosc1030\lab11\lab11\lab11\auto.cpp(14): error C2084: function 'Auto::Auto(void)' already has a body
1> m:\cosc1030\lab11\lab11\lab11\auto.h(18) : see previous definition of '{ctor}' 1>m:\cosc1030\lab11\lab11\lab11\auto.cpp(20): error C2084: function 'Auto::Auto(const char *,const char *,int)' already has a body
1> m:\cosc1030\lab11\lab11\lab11\auto.h(19) : see previous definition of '{ctor}'

Class definition in header file issues

I am receiving the following error from the compiler
./include.list.h(22) error: identifier "input" is undefined.
Here is the header file where I explicitly define input-
#ifndef INIT_H
#define INIT_H
#include "list.h"
class input{
public:
//** thermo variables **//
int N;
double nddensity;
double ndtemp;
double ndvol;
double ndside;
double ndsideh;
// ** force field variables** //
double eps;
double sigma;
double rcut;
double rv;
double rcut2;
double rv2;
double rcrv2;
input();
};
void print(input &);
double randomnumber();
void position(list &, input &, int flag);
#endif
In init.cpp, I have the class initialization:
input:: input() {
//** thermo variables **//
N = 500;
nddensity =.8;
ndtemp = 2.0;
ndvol = N/nddensity;
ndside = pow(ndvol,1.0/3.0);
ndsideh = ndside/2;
// ** force field variables** //
eps = 1;
sigma = 1;
rcut = 2.5;
rv = 1.1*rcut;
rcut2 = rcut*rcut;
rv2 = rv*rv;
rcrv2 = (rv-rcut)*(rv-rcut);
}
I can't seem to figure out why input would be undefined to void print. Any help is appreciated.
You have a circular dependency.
You can forward declare the other class, but this is still a poor design. Better to just do away with the circular dependency altogether. At second glance, do you even need to include list.h? Where are you using anything declared in it?
You include list.h before the class definition, so it's not available in that header. According to the error message, something in list.h needs a declation of the class.
You don't need a full definition of list here, so replace the #include with a forward declaration
class list;
Within list.h, you probably need a similar declaration of input; or it might be necessary to include init.h if the header does something complicated with it.

c++ Set not compiling properly

I am creating a data structure but when I try and compile I get an error saying that I haven't specified that type of set that I am initializing.
I am working with the NTL library with is used for large numbers.
This is my code:
#include <set>
#include ...
NTL_CLIENT
using namespace std;
using namespace NTL;
const RR ZERO = to_RR(0);
const RR ONE = to_RR(1);
const RR TWO = to_RR(2);
class tenTree
{
public:
tenTree(string newName = "", int newLevel = 0);
~tenTree();
void put(string prefix, RR power);
bool get(string prefix, RR & output);
void display(int depth);
bool isKnown(RR power){return (powers.find(power) != powers.end());};
private:
tenTree* children [10];
set<int> powers;
int level;
string name;
bool child[10];
};
When I try to compile it comes back with an error saying:
twoPow.cpp:47: error: ISO C++ forbids declaration of \u2018set\u2019 with no type
twoPow.cpp:47: error: expected \u2018;\u2019 before \u2018<\u2019 token
twoPow.cpp: In member function \u2018bool tenTree::isKnown(NTL::RR)\u2019:
twoPow.cpp:44: error: \u2018powers\u2019 was not declared in this scope
Is there something that I am missing here?
It was just a matter of the scope. All I had to do was add an std:: before the set and it compiled correctly.

Unresolved External Symbol

I have encountered an error which has stumped me for many days now. A quick google hasn't given me an answer. The code, to my eyes, has no errors however when I run the program I get 9 Unresolved External Symbol(LNK2019) error. After trying to decipher one of my errors, I believe it is happening in a function named createMortgage. Here is my calling of the function.
customers is a Vector.
for (unsigned int i = 0; i < customers.size(); i++)
{
Customer tempcust = customers.at(i);
if (tempcust.getId() == id)
{
customers.at(i).createMortgage();
}
}
Here is the function itself.
void createMortgage(){
int amount;
cout << "Amount?";
cin >> amount;
Mortgage mort(amount);
mortgages.push_back(mort);
}
And here, in all it's glory, is the error.
Error 4 error LNK2019: unresolved external symbol "public: __thiscall Mortgage::Mortgage(double)" (??0Mortgage##QAE#N#Z) referenced in function "public: void __thiscall Customer::createMortgage(void)" (?createMortgage#Customer##QAEXXZ) F:\C++ assignment (Real)\C++ assignment (Real)\Driver.obj C++ assignment (Real)
Here is my mortgage .h file.
#pragma once
//#include <iostream>
//#include <String>
class Mortgage
{
private:
int id;
double amount;
public:
Mortgage(double amount);
double getAmount();
int getId();
};
And here is my mortgage .cpp file.
#pragma once
extern int idcreation;
class Mortgage
{
int id;
double amount;
Mortgage(double amount)
{
this -> amount = amount;
this -> id = idcreation;
idcreation++;
}
int getId(){
return id;
}
double getAmount(){
return amount;
}
Change this:
class Mortgage
{
int id;
double amount;
Mortgage(double amount)
{
this -> amount = amount;
this -> id = idcreation;
idcreation++;
}
int getId(){
return id;
}
double getAmount(){
return amount;
}
To this:
#include "mortgage.h"
Mortgage::Mortgage(double amount)
{
this -> amount = amount;
this -> id = idcreation;
idcreation++;
}
int Mortgage::getId(){
return id;
}
double Mortgage::getAmount(){
return amount;
}
I see you don't really get how to use headers and source files to make classes, this tutorial will get you on track: http://thenewboston.org/watch.php?cat=16&number=15.
1) You aren't linking (and probably not compiling) your mortgage.cpp file. Check your IDE project configuration to ensure that it includes mortgage.cpp as a source file.
2) You must not reproduce the class definition in your cpp file. Rather, structure it like this:
#include "mortgage.h"
Mortage::Mortgage(double d) { ... }
You have issues with basic C++ syntax.
#pragma once is Visual Studio specific and is a replacement for header guards. It should never appear in a .cpp file
You are providing two different definitions of class Mortage one is in the header, the second one is in the .cpp file
The correct syntax for defining class is the following:
The header file:
/* something.h */
#ifndef SOMETHING_H_
#define SOMETHING_H_
class Something
{
public:
Something();
void some_method();
};
#endif
The .cpp file:
/* something.cpp */
#include "something.h"
Something::Something() { /* implementation */ }
void Something::some_method() { /* implementation */ }

class issue in C++

I have this in furniture.h:
#include <iostream>
#include <string>
using namespace std;
class Furniture {
public:
Furniture();
virtual ~Furniture();
void setname(string name);
void setprice(double price);
int getprice();
string getname();
private:
string name;
int price;
protected:
static int NumberOfItems;
int Id;
}
and this in furniture.cpp
#include "furniture.h"
void Furniture::setname(string name) {
this->name = name;
}
string Furniture::getname()
{
return this->name;
}
void Furniture::setprice(double price) {
this->price = price;
}
int Furniture::getprice() {
return this->price;
}
int main() {
Furniture *model = new Furniture();
model->setname("FinalDestiny");
model->setprice(149.99);
cout<<"Model name: "<<model->getname()<<" - price = "<<model->getprice();
}
But I get some errors like:
Error 1 error C2628: 'Furniture' followed by 'void' is illegal (did you forget a ';'?) c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 2 error C2556: 'Furniture Furniture::setname(std::string)' : overloaded function differs only by return type from 'void Furniture::setname(std::string)' c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 3 error C2371: 'Furniture::setname' : redefinition; different basic types c:\final\facultate\poo\laborator 1\furniture.cpp 3 1 POO_lab
Error 5 error C2264: 'Furniture::setname' : error in function definition or declaration; function not called c:\final\facultate\poo\laborator 1\furniture.cpp 19 1 POO_lab
What am I doing wrong?
You are missing a ; at the end of the class definition in your header file.
// ...snipped...
protected:
static int NumberOfItems;
int Id;
}; // <-- here
You've forgotten a semicolon at the end of your class definition.
// ...
protected:
static int NumberOfItems;
int Id;
}; // <--
I hate that about C++ :)
Two things;
You're not ending your class definition with a ;, you need one at the end of furniture.h.
You've declared that there's a constructor and destructor, but neither is implemented in your .cpp file.