user2791464
user2791464

Reputation:

Error compiling c++ tutorial (programmingHelp.org)

I am following tutorials from youtube. It seems I have encountered an error I can't resolve by myself. The goal is to create a class called BMI, which takes users weight name and height and prints them out..

I'm trying to compile it using g++, and I suspect I'm not doing it right. Usually I just do g++ filename.cpp, as I should in this case?

The tutorial is originally in that Microsoft ....thing, I don't know it's name. Sorry

Thank you, the code is attached below.

The error

/tmp/ccRcewk3.o: In function `main':
Main.cpp:(.text+0x7d): undefined reference to `BMI::BMI()'
Main.cpp:(.text+0x89): undefined reference to `BMI::getWeight() const'
Main.cpp:(.text+0x9a): undefined reference to `BMI::getHeight() const'
Main.cpp:(.text+0xaf): undefined reference to `BMI::getName() const'
Main.cpp:(.text+0x14f): undefined reference to `BMI::~BMI()'
Main.cpp:(.text+0x184): undefined reference to `BMI::~BMI()'
collect2: ld returned 1 exit status

Main.cpp

#include <iostream>
#include <string>

#include "BMI.h"

using namespace std;

/**************************************************/
int main()
{
  string name;
  int height;
  double weight;

  cout << "Enter your name: ";
  cin >> name;

  cout << "Enter your height (cm): ";
  cin >> height;

  cout << "Enter your weight (kg): ";
  cin >> weight;

  BMI Student_1;

  cout << endl << "Patient name: " << Student_1.getName() << endl <<
    "Height: " << Student_1.getHeight() << endl <<
    "Weight: " << Student_1.getWeight() << endl;
  return 0; 
}
/**************************************************/

BMI.h

// Header ==> Function Declarations

#include <iostream>
#include <string>

using namespace std;

// tu ide klasa
#ifndef BMI_H
#define BMI_H

class BMI
{
 public:
  //Default Constructor
  BMI();

  //Overload Constructor
  BMI(string, int, double);

  //Destructor
  ~BMI();

  // Accessor functions
  string getName() const;
  // // // returns name of patient

  int getHeight() const;
  // // // returns height of patient

  double getWeight() const;
  // // // returns weight of patient


 private:
  // member variables
  string newName;
  int newHeight;
  double newWeight;

};

#endif

BMI.cpp:

//Function definitions

#include "BMI.h"

// to access function inside a class

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;
}

int BMI::getWeight() const
{
  return newWeight;
}

edit: OK, thanks everyone, I got part of the problem solved. However, you got me a little confused with editing, so I will do over.

It seems that the original code is not working, and I feel it should. Anyway, the edited code from the question doesn't work either.

So, I will try to do it again. But thank you, now I know how to compile. :)

edit2: Everything is working now, thank you very much.

Upvotes: 0

Views: 319

Answers (2)

mvw
mvw

Reputation: 5105

You need to compile the main.cpp into Main.o and BMI.cpp into BMI.o.

g++ -c Main.cpp
g++ -c BMI.cpp

Then you need to link both object files into one executable (and link to the Standard C++ lib)

g++ -o myprog Main.o BMI.o -lstdc++

Run the example with

./myprog

There seem to be more bugs, I have no time to fix, please continue yourself. :-)

[marc@quadfork ~/test]$./myprog
Enter your name: foo
Enter your height (cm): 23
Enter your weight (kg): 2

Patient name:
Height: 0
Weight: 0

Upvotes: 2

user1990
user1990

Reputation: 546

your function return noting in your BMI.cpp try with this.

string BMI::getName() const
{
  return newName;
}

int BMI::getHeight() const
{
  return newHeight;
}

double BMI::getWeight() const
{
  return newWeight;
}

Upvotes: 1

Related Questions