Solved

class A

Posted on 2004-04-27
10
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

734 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