473,385 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Class does not cout from constructor?

I am a complete newbie to C++ (I primarily work in Java & PHP).

I expect this code to print Making a new: Alien when I run it.
It compiles fine, and runs, but it just prints Making a new:
No Alien :(

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Alien{
  5.     public:
  6.         string name;
  7.         Alien(){
  8.             cout << "Alien";
  9.         };
  10. };
  11.  
  12. int main(){
  13.     cout << "Making a new: ";
  14.     Alien al();
  15.     return 0;
  16. }
Sep 6 '07 #1
2 7448
Banfa
9,065 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Alien{
  5.     public:
  6.         string name;
  7.         Alien(){
  8.             cout << "Alien";
  9.         };
  10. };
  11.  
  12. int main(){
  13.     cout << "Making a new: ";
  14.     Alien al();
  15.     return 0;
  16. }
You error is on this line
Expand|Select|Wrap|Line Numbers
  1.     Alien al();
this is interpreted as a function prototype, that is you have declared a function that takes no parameters and returns type Alien. This links because the function is not actually called so it does not have to exist.

To instantiate an alien using the default constructor you should change this to

Expand|Select|Wrap|Line Numbers
  1.     Alien al;
Additionally you never output an end of line or flush stdout. Output is buffered and the data written to stdout is only guarenteed to be written when expicitly flushed or an end of line character is see.
Sep 6 '07 #2
I think you should use somethin like

al.Alien();

in your main()

I tried to run the following code with Dev-C++ and it works:

Expand|Select|Wrap|Line Numbers
  1.  #include <iostream>
  2. #include <conio.h>
  3. using namespace std;
  4.  
  5. class Alien{
  6.  
  7.     public:
  8.         string name;
  9.         void Alien_view(void){
  10.             cout << "Alien";
  11.         };
  12. };
  13.  
  14. int main(){
  15.     cout << "Making a new: ";
  16.     Alien al;
  17.     al.Alien_view();
  18.     getch();
  19.     return 0;
  20. }
Depending on your compiler you may be not allowed to avoid returned and parameter data type in class method declaration.

diSangro

I am a complete newbie to C++ (I primarily work in Java & PHP).

I expect this code to print Making a new: Alien when I run it.
It compiles fine, and runs, but it just prints Making a new:
No Alien :(

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Alien{
  5.     public:
  6.         string name;
  7.         Alien(){
  8.             cout << "Alien";
  9.         };
  10. };
  11.  
  12. int main(){
  13.     cout << "Making a new: ";
  14.     Alien al();
  15.     return 0;
  16. }
Sep 6 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
7
by: Robin Forster | last post by:
I have two classes: aule_gl_window (parent class) and aule_button (sub class) I want to call the super class (parent) constructor code from the sub class constructor.
5
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
11
by: DrNoose | last post by:
Hi! I've got a program that's almost done, but I'm getting compile errors in two lines: 317 & 319. I think the main error has to do with the Truck Class. I'm a newbie and keep looking at the...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
11
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
12
by: hweekuan | last post by:
hi, it seems i can't assign the const variable u in class A, one way to solve the problem may be to build a copy constructor. however, why does C++ or vector class not like this code? my g++ is:...
8
by: hill.liu | last post by:
Hi, I stuck into this problem that I can't figure it out. Here is the class definition: class ctest { public: ctest(void) { cout << "ctest default constor" << endl; }; ctest(ctest& c) {...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.