Copy constructor

Apr 15, 2015 at 2:02pm
Hello! Please, I tried to copy the first object using copy constructor and I do nto see mistake! Why is "another " not declared in this scope? Many thanks!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  P#include<iostream>
using namespace std;

class BMI{
  public:
   string mynameis;
   string mysurnameis;
  private:
   int myheightis;
   int myweightis;
 public:

  BMI(string, string, int, int);
  BMI (const BMI& another); //COPY CONSTRUCTOR
  int bodymassindex(){ return myheightis*myweightis;}
  
};

BMI::BMI(string a, string b, int c, int d){
mynameis=a;
mysurnameis=b;
myheightis=c;
myweightis=d;
}

BMI::BMI(const BMI& another) : mynameis(another.mynameis), mysurnameis(another.mysurnameis), myheightis(another.myheightis), myweightis(another.myweightis) {}




int main(){
BMI an ("a", "aaa", 10,20);
BMI marry("m", "mmm", 40, 50);
BMI nelly("n", "nnn", 80, 90);

cout<<an.mynameis<<": body mass index is:  "<<an.bodymassindex()<<endl;

cout<<another.mynameis<<": body mass index is:  "<<another.bodymassindex()<<endl;
return 0;
}


Please, ignore the wrong BMI formula!
MANY THANKS!!!
Apr 15, 2015 at 2:05pm
Because it wasnt? Look at your main. Where do you create "another" ?. You create nelly, marry and an, but never something called another.
Apr 15, 2015 at 2:22pm
1
2
3
4
5
6
7
8
9
BMI::BMI(const BMI& another) : mynameis(another.mynameis), mysurnameis(another.mysurnameis), myheightis(another.myheightis), myweightis(another.myweightis)
{
    cout<<another.mynameis<<": body mass index is:  "<<another.bodymassindex()<<endl;
}

int main(){
    BMI an ("a", "aaa", 10,20);
    BMI anCopy(an);
    //... 
Apr 18, 2015 at 5:04pm
Thanks!!!!!
Topic archived. No new replies allowed.