newbie question

M

mrkafk

Hello everyone,

I'm very much (greenish) new to C++, learning it with S. Lippman's "C+
+ Primer" in hand.

I tried to extend one example:


#include <iostream>
using namespace std;

class IntArray
{
public:
explicit IntArray(int sz = DefaultArraySize);
IntArray(int *array, int array_size);
int siz();
private:
static const int DefaultArraySize = 12;
int _size;
int* ia;
};

IntArray::IntArray(int sz)
{
_size = sz;
ia = new int[_size];
for (int i=0; i<_size; i++)
ia=0;
}

IntArray::siz()
{
return _size;
}

int main()
{
IntArray a(5);
cout << "a.siz() : " << a.siz() << endl;
}

Yet, g++ complains:

e.C:25: error: ISO C++ forbids declaration of 'siz' with no type

? I don't get it? After all, I have declared siz() to be of int type
in the class declaration? I can't use IntArray::int siz(), can I?

(of course I tried it and get e.C:25: error: expected unqualified-id
before 'int' instead, doomed if I do and doomed if I don't)
 
K

Klaas Vantournhout

Hello everyone,

I'm very much (greenish) new to C++, learning it with S. Lippman's "C+
+ Primer" in hand.

I tried to extend one example:


#include <iostream>
using namespace std;

class IntArray
{
public:
explicit IntArray(int sz = DefaultArraySize);
IntArray(int *array, int array_size);
int siz();
private:
static const int DefaultArraySize = 12;
int _size;
int* ia;
};

IntArray::IntArray(int sz)
{
_size = sz;
ia = new int[_size];
for (int i=0; i<_size; i++)
ia=0;
}

IntArray::siz()
{
return _size;
}

int main()
{
IntArray a(5);
cout << "a.siz() : " << a.siz() << endl;
}

Yet, g++ complains:

e.C:25: error: ISO C++ forbids declaration of 'siz' with no type

? I don't get it? After all, I have declared siz() to be of int type
in the class declaration? I can't use IntArray::int siz(), can I?



In your class definition you have indeed defined IntArray::siz(void) to
return return an int, but in your function definition itself you forgot
the int part. you have to state what the function returns.

So place

int IntArray::siz() {
return _size;
}

and the problem should be resolved.
 
D

David Côme

Hello everyone,

I'm very much (greenish) new to C++, learning it with S. Lippman's "C+
+ Primer" in hand.

I tried to extend one example:


#include <iostream>
using namespace std;

class IntArray
{
public:
explicit IntArray(int sz = DefaultArraySize);
IntArray(int *array, int array_size);
int siz();
private:
static const int DefaultArraySize = 12;
int _size;
int* ia;
};

IntArray::IntArray(int sz)
{
_size = sz;
ia = new int[_size];
for (int i=0; i<_size; i++)
ia=0;
}

IntArray::siz()
{
return _size;
}

int main()
{
IntArray a(5);
cout << "a.siz() : " << a.siz() << endl;
}

Yet, g++ complains:

e.C:25: error: ISO C++ forbids declaration of 'siz' with no type

? I don't get it? After all, I have declared siz() to be of int type
in the class declaration? I can't use IntArray::int siz(), can I?

(of course I tried it and get e.C:25: error: expected unqualified-id
before 'int' instead, doomed if I do and doomed if I don't)


All function must have a return-type (except constructor and destrcuteur).
So IntArray::siz() become int IntArray::siz().

:D
 
K

Klaas Vantournhout

Klaas said:
Hello everyone,

I'm very much (greenish) new to C++, learning it with S. Lippman's "C+
+ Primer" in hand.

I tried to extend one example:


#include <iostream>
using namespace std;

class IntArray
{
public:
explicit IntArray(int sz = DefaultArraySize);
IntArray(int *array, int array_size);
int siz();
private:
static const int DefaultArraySize = 12;
int _size;
int* ia;
};

IntArray::IntArray(int sz)
{
_size = sz;
ia = new int[_size];
for (int i=0; i<_size; i++)
ia=0;
}

IntArray::siz()
{
return _size;
}

int main()
{
IntArray a(5);
cout << "a.siz() : " << a.siz() << endl;
}

Yet, g++ complains:

e.C:25: error: ISO C++ forbids declaration of 'siz' with no type

? I don't get it? After all, I have declared siz() to be of int type
in the class declaration? I can't use IntArray::int siz(), can I?



In your class definition you have indeed defined IntArray::siz(void) to
return return an int, but in your function definition itself you forgot
the int part. you have to state what the function returns.

So place

int IntArray::siz() {
return _size;
}

and the problem should be resolved.



Something else I noticed is that your main part of the program (the
function int main(void), does not have a return type.

Here you should return the errorcode.

If your program finishes successfull you should finish the main part of
your code with return 0;, If it hangs somwehere in the middle of your
code, give it an other number. This way you can see if your program
finished succesfully after it finished.

Hence
int main() {
IntArray a(5);
cout << "a.siz() : " << a.siz() << endl;
return 0;
}
 
A

Abhishek Padmanabh

Klaas said:
Hello everyone,
I'm very much (greenish) new to C++, learning it with S. Lippman's "C+
+ Primer" in hand.
I tried to extend one example:
#include <iostream>
using namespace std;
class IntArray
{
public:
 explicit IntArray(int sz = DefaultArraySize);
 IntArray(int *array, int array_size);
 int siz();
private:
 static const int DefaultArraySize = 12;
 int _size;
 int* ia;
};
IntArray::IntArray(int sz)
{
 _size = sz;
 ia = new int[_size];
 for (int i=0; i<_size; i++)
        ia=0;
}
IntArray::siz()
{
 return _size;
}
int main()
{
 IntArray a(5);
 cout << "a.siz() : " << a.siz() << endl;
}
Yet, g++ complains:
 e.C:25: error: ISO C++ forbids declaration of 'siz' with no type
? I don't get it? After all, I have declared siz() to be of int type
in the class declaration? I can't use IntArray::int siz(), can I?

In your class definition you have indeed defined IntArray::siz(void) to
 return return an int, but in your function definition itself you forgot
the int part.  you have to state what the function returns.
int IntArray::siz() {
return _size;
}
and the problem should be resolved.

Something else I noticed is that your main part of the program (the
function int main(void), does not have a return type.

Here you should return the errorcode.

If your program finishes successfull you should finish the main part of
your code with return 0;, If it hangs somwehere in the middle of your
code, give it an other number.  This way you can see if your program
finished succesfully after it finished.

Hence
int main() {
 IntArray a(5);
 cout << "a.siz() : " << a.siz() << endl;
 return 0;
}


If the return is missing, it is implicitly equivalent to return 0;
 
S

Salt_Peter

Klaas said:
Hello everyone,
I'm very much (greenish) new to C++, learning it with S. Lippman's "C+
+ Primer" in hand.
I tried to extend one example:
#include <iostream>
using namespace std;
class IntArray
{
public:
explicit IntArray(int sz = DefaultArraySize);
IntArray(int *array, int array_size);
int siz();
private:
static const int DefaultArraySize = 12;
int _size;
int* ia;
};
IntArray::IntArray(int sz)
{
_size = sz;
ia = new int[_size];
for (int i=0; i<_size; i++)
ia=0;
}
IntArray::siz()
{
return _size;
}
int main()
{
IntArray a(5);
cout << "a.siz() : " << a.siz() << endl;
}
Yet, g++ complains:
e.C:25: error: ISO C++ forbids declaration of 'siz' with no type
? I don't get it? After all, I have declared siz() to be of int type
in the class declaration? I can't use IntArray::int siz(), can I?

In your class definition you have indeed defined IntArray::siz(void) to
return return an int, but in your function definition itself you forgot
the int part. you have to state what the function returns.
int IntArray::siz() {
return _size;
}
and the problem should be resolved.

Something else I noticed is that your main part of the program (the
function int main(void), does not have a return type.

Here you should return the errorcode.

If your program finishes successfull you should finish the main part of
your code with return 0;, If it hangs somwehere in the middle of your
code, give it an other number. This way you can see if your program
finished succesfully after it finished.

Hence
int main() {
IntArray a(5);
cout << "a.siz() : " << a.siz() << endl;
return 0;

}


This is not required. Supplying it doesn't solve anything anyways
since the program's return value is ignored. A much better alternative
is to use exceptions to handle exceptional conditions.
 
E

Erik Wikström

Klaas said:
(e-mail address removed) wrote:
Hello everyone,
I'm very much (greenish) new to C++, learning it with S. Lippman's "C+
+ Primer" in hand.
I tried to extend one example:
#include <iostream>
using namespace std;
class IntArray
{
public:
explicit IntArray(int sz = DefaultArraySize);
IntArray(int *array, int array_size);
int siz();
private:
static const int DefaultArraySize = 12;
int _size;
int* ia;
};
IntArray::IntArray(int sz)
{
_size = sz;
ia = new int[_size];
for (int i=0; i<_size; i++)
ia=0;
}

IntArray::siz()
{
return _size;
}
int main()
{
IntArray a(5);
cout << "a.siz() : " << a.siz() << endl;
}
Yet, g++ complains:
e.C:25: error: ISO C++ forbids declaration of 'siz' with no type
? I don't get it? After all, I have declared siz() to be of int type
in the class declaration? I can't use IntArray::int siz(), can I?
In your class definition you have indeed defined IntArray::siz(void) to
return return an int, but in your function definition itself you forgot
the int part. you have to state what the function returns.
int IntArray::siz() {
return _size;
}
and the problem should be resolved.

Something else I noticed is that your main part of the program (the
function int main(void), does not have a return type.

Here you should return the errorcode.

If your program finishes successfull you should finish the main part of
your code with return 0;, If it hangs somwehere in the middle of your
code, give it an other number. This way you can see if your program
finished succesfully after it finished.

Hence
int main() {
IntArray a(5);
cout << "a.siz() : " << a.siz() << endl;
return 0;

}


This is not required. Supplying it doesn't solve anything anyways
since the program's return value is ignored. A much better alternative
is to use exceptions to handle exceptional conditions.


You are speaking about two different things, exceptions should be used
to handle exceptional conditions within the program, but sometimes there
is no other choice than to terminate the program. In those cases a non-
zero return value should be used.

As for the return value being ignored; that is not true, on Unix/Linux
systems the return value can be very useful, especially when writing
command line utilities which might be used by scripts, but also on
Windows systems where batch-files or scripts are used.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top