Solved

class A

Posted on 2004-04-27
10
255 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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
 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

816 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

12 Experts available now in Live!

Get 1:1 Help Now