Solved

class A

Posted on 2004-04-27
10
253 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:edelossantos
Comment Utility
#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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 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

11 Experts available now in Live!

Get 1:1 Help Now