Solved

class A

Posted on 2004-04-27
10
254 Views
Last Modified: 2010-04-01
#include <iostream>

using namespace std;

class A {

public:

   A(int x, double y, char z) {a=x;
                               b=y;
                               c=z;
                                        }

   ~A();

private:

   int a;
   double b;
   char c;

};

int main()
{
   int obj(2, 20.0, 'a');
   int obj_2;

   obj_2 = obj;

}

output:

classA.cpp: In function `int main()':
classA.cpp:26: initializer list being treated as compound expression
0
Comment
Question by:edelossantos
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 125 total points
ID: 10935689
I think your bymistake wrote the main incorrectly

int main()
{
   int obj(2, 20.0, 'a');
   int obj_2;

   obj_2 = obj;

}

should be

int main()
{
   A obj(2, 20.0, 'a');
   A obj_2;

   obj_2 = obj;

}

Amit
0
 
LVL 11

Assisted Solution

by:avizit
avizit earned 125 total points
ID: 10935709
int main()
{
   int obj(2, 20.0, 'a');
   int obj_2;

   obj_2 = obj;

}


--
 you possibly want obj and obj_2 as type of class A and not int

/abhijit/

0
 
LVL 11

Assisted Solution

by:avizit
avizit earned 125 total points
ID: 10935733
and one more thing you haven't defined the destructor


you can do two things

1. dont declare any ( cos its a simple class and the compiler can do the needful ). So you can just remove the line  ~A();

2. declare but  keep it empty i.e change the line  ~A();  to ~A(){}

/abhijit/
0
 

Author Comment

by:edelossantos
ID: 10935753
Now I am getting this, couldn't figure out.

#include <iostream>

using namespace std;

class A {

public:

   A(int x, double y, char z) {a=x;
                         b=y;
                         c=z;
                              }

   ~A();

private:

   int a;
   double b;
   char c;

};

int main()
{
   A obj(2, 20.0, 'a');
   A obj_2;

   obj_2 = obj;

}


output:

classA.cpp: In function `int main()':
classA.cpp:27: no matching function for call to `A::A ()'
classA.cpp:9: candidates are: A::A(int, double, char)
classA.cpp:22:                 A::A(const A &)
0
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 125 total points
ID: 10935768
Thats because u do not have a default constructor for class A

int main()
{
   A obj(2, 20.0, 'a');
   A obj_2;          // This would try to invoke the default constructor, hence error because no default constructor

   obj_2 = obj;     // This assigns obj to obj_2

}

can be

int main()
{
   A obj(2, 20.0, 'a');
   A obj_2 = obj;            // No default constructir needed, directly compiler synthesized copy constructor used
}

Amit
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:edelossantos
ID: 10935858
#include <iostream>

using namespace std;

class A {

public:

   A(int x, double y, char z){
        a = x;
        b = y;
        c = z;

   }

   ~A(){};

private:

   int a;
   double b;
   char c;

};

int main()
{
   A obj(2, 20.0, 'a');
   A obj_2;


}

output:

classA.cpp: In function `int main()':
classA.cpp:29: no matching function for call to `A::A ()'
classA.cpp:9: candidates are: A::A(int, double, char)
classA.cpp:24:                 A::A(const A &)
0
 
LVL 11

Assisted Solution

by:avizit
avizit earned 125 total points
ID: 10935937
as amit said yuo dont have default constructors

one way would be to

change A(int x, double y, char z){
        a = x;
        b = y;
        c = z;

   }

to
A(int x = 0, double y = 0.0 , char z = ' '){
        a = x;
        b = y;
        c = z;

   }

== btw dont just copy what we tell here to your code and compile

read some literature to understand it too.

here in thise case your contructors work only when you supply 3 arguments to it. what if you dont supply any ? the code i gave wil substitute the default values.

/abhijit/
0
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 125 total points
ID: 10935951
The same thing as i said in previous comment

In C++, if u create a class with no constructor, the compiler provides u a default constructor
If u provide a constructor taking 1/more parameter(s) with at least one parameter not having default, then u have to provide a default constructor OR u cannot create a object with no parameters

Example

class A {
}

A obj1 ;     // OK, compiler provides default

class B {
       int i ;
        int j ;
     public :
        B ( int a, int b ) : i ( a ), j ( b ) {}
} ;

B obj1 ( 2,3 ) ; // OK
B obj2 ;     // Error , no default constructor
B obj3 = obj1 ; //OK compiler provides Copy constructor

Amit
0
 
LVL 10

Assisted Solution

by:Sys_Prog
Sys_Prog earned 125 total points
ID: 10935976
Continuing my above example

class B {
       int i ;
        int j ;
     public :
        B ( int a  , int b = 100 ) : i ( a ), j ( b ) {}
} ;

B obj1 ( 2,3 ) ; // OK
B obj2 ;     // Error , no default constructor
B obj3 = obj1 ; //OK, compiler provides Copy constructor
B obj4 ( 10 ) ;      // OK, 10 is used as first parameter, and 100 as the second parameter to the constructor (because of default value)


Amit
0
 
LVL 4

Accepted Solution

by:
PerryDK earned 250 total points
ID: 10935983
OUTPUT:
obj 2, 20, a
obj_2 before assignment 0, 0,
obj_2 after assignment 2, 20, a


ACTUAL CODE
#include <iostream>

using namespace std;

class A {

public:
  A()
  {
    a = 0;
    b = 0;
    c = 0;
  }
  A(int x, double y, char z)
  {
    a=x;
    b=y;
    c=z;
  }

  ~A()
  {
  }

  friend ostream& operator << (ostream& out, const A& a)
  {
    out << a.a << ", " << a.b << ", " << a.c << endl;
    return out;
  }

private:

   int a;
   double b;
   char c;

};


int main()
{
   A obj(2, 20.0, 'a');
   A obj_2;

   cout << "obj " << obj;

   cout << "obj_2 before assignment " << obj_2;

   obj_2 = obj;

   cout << "obj_2 after assignment " << obj_2;

}



0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now