Copy constructor

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!!!
Because it wasnt? Look at your main. Where do you create "another" ?. You create nelly, marry and an, but never something called another.
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);
    //... 
Thanks!!!!!
Topic archived. No new replies allowed.