?
Solved

C++ syntax errors

Posted on 2005-05-09
17
Medium Priority
?
270 Views
Last Modified: 2010-04-01
The following is a piece of code with some syntax errors and possible a runtime error. I am not at all familiar with C++ and i would be grateful if someone could identify these errors for me.
Thank you!
Diarmuid

%include <iostream.h>

class parent {

private:
    int  size;
    int* array;

public:
    // Constructor
    void parent(int k) : size(k) {
       array = new int(k);
    }

    void ChangeArray(int index, int val) {
       array[index] = val;
    }

    // Destructor
    ~parent(void)
    {
        delete size;
    }
}

class child : private parent {

private:
    int index;

public:
    // Constructor
    child(int a, int b, int c) : parent(a, b) {
        index = c;
    }

    int get_size(void) {
        return size;
    }
};
0
Comment
Question by:diarmuidflaherty
  • 8
  • 4
  • 4
  • +1
17 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13959250
%include <iostream.h>
must be
#include <iostream>
0
 
LVL 30

Expert Comment

by:Axter
ID: 13959298
FYI:

<iostream.h> is not part of the C++ standard, and should not be used.
You should use <iostream> which is part of the C++ standard, and therefore portable.

0
 

Author Comment

by:diarmuidflaherty
ID: 13959423
Thanks guys! but i think there is about 8 errors. can ye see any more????
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13959497
You need a semicolon at the end of 'parent' class declaration, just like with 'child' class
The parent destructor must not try to delete 'size', since it is not a pointer, you have to delete array instead.
0
 
LVL 30

Expert Comment

by:Axter
ID: 13959504
>>Thanks guys! but i think there is about 8 errors. can ye see any more????

Are you getting these other errors after you fixed the above problem?

Compilers will often give false errors after being thrown off by a syntax error, so your other additional errors may not exist.

Recompile the code, and if you still get errors, post the exact errors here so we can see exactly what the compiler is complaining about.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13959516
   void parent(int k) : size(k) {

you don't have to specify return type in a constructor, must be:

    parent(int k) : size(k) {

----------------------

    child(int a, int b, int c) : parent(a, b) {

parent constructor doesn't receive 2 arguments, just 1
0
 
LVL 30

Accepted Solution

by:
Axter earned 100 total points
ID: 13959520
You should initialize array to NULL in your constructor.

Otherwise when you delete it in your destructor, you'll get undefined behavior.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13959535
>You should initialize array to NULL in your constructor.
>Otherwise when you delete it in your destructor, you'll get undefined behavior

why? although it is a good practice, unique constructor ALLWAYS initialize array.
0
 

Author Comment

by:diarmuidflaherty
ID: 13959919
sorry i don't even hav a c++ compiler, so i hav no idea what errors i would be gettin. The only thing i know is that there are 8 errors in the original piece of code i posted.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13959983
Hmmm, sound like we are making your homework...
0
 

Author Comment

by:diarmuidflaherty
ID: 13960068
Actually i hav an exam tomorrow, i'm looking over past questions....
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13960087
Let's do this, you send your answer and we can correct it. If you don't have an idea about how to answer then better don't go to exam tomorrow.
0
 
LVL 30

Expert Comment

by:Axter
ID: 13960090
>>why? although it is a good practice, unique constructor ALLWAYS initialize array.

I missed that.
Although I recommend to always initialize pointers to NULL in initialize list.
Moreover, you can just initialize the pointer in the initialize list, just like the size variable.
0
 

Author Comment

by:diarmuidflaherty
ID: 13960120
it's ok guys
i'll figure it out when i get home to my machine when i can use a compiler.
who are you MY MOTHER??????
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13960139
>who are you MY MOTHER??????
No, I am try to not violate EE rules because I really dislike to receive a message from moderator.
0
 

Expert Comment

by:liamgannon
ID: 13960147
I agree with jaime_olivares, this sounds a bit suspect. Maybe you should have done some real revision
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13960169
You have accepted the wrong answer, this also violates EE rules.
0

Featured Post

Independent Software Vendors: 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!

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.
Suggested Courses

749 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