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
Solved

class A

Posted on 2004-04-27
10
256 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

792 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