Click to See Complete Forum and Search --> : Functions in C++
agatonsax
07-28-2005, 06:13 PM
I'm trying to learn C++ and I'm a total newbie and have the book "Sams Teach yourself C++ in 21 days" and when I come to listing 2.6 where they demonstrate functions I run into trouble. The list goes like this (the actual listing has void
DemonstrationFunction() but that doesn't work for the same reason, not allowed. Changing it to int DemonstrationFunction() and the problem remains)
#include <iostream>
DemonstrationFunction()
{
std::cout << "In Demonstration Function\n";
}
int main()
[
std::cout << "In main\n";
DemonstrationFunction();
std::cout << "Back in main\n";
return 0;
}
and when I compile it says (all errors)
listing26.cpp:4: error: ISO C++ forbids declaration of `DemonstrationFunction'
with no type
listing26.cpp:10: error: parsningsfel before `;' token
listing26.cpp:11: error: ISO C++ forbids declaration of `DemonstrationFunction'
with no type
listing26.cpp:12: error: syntaxfel before `<<' token
I guess the errors after the first depend on the first one.
How do I call a simple function like that DemonstrationFunction?
bwkaz
07-28-2005, 07:20 PM
My installation of g++ will compile this successfully:
#include <iostream>
void DemonstrationFunction()
{
std::cout << "In Demonstration Function\n";
}
int main()
{
std::cout << "In main\n";
DemonstrationFunction();
std::cout << "Back in main\n";
return 0;
} (I.e., replace the [ with a { after main, and put a void before DemonstrationFunction().) Command I used to compile:
g++ -o listing26 listing26.cpp
celticgeek
07-28-2005, 07:35 PM
I'm trying to learn C++ and I'm a total newbie and have the book "Sams Teach yourself C++ in 21 days" and when I come to listing 2.6 where they demonstrate functions I run into trouble. The list goes like this (the actual listing has void
DemonstrationFunction() but that doesn't work for the same reason, not allowed. Changing it to int DemonstrationFunction() and the problem remains)
#include <iostream>
DemonstrationFunction() //This must have a return type of void
{
std::cout << "In Demonstration Function\n";
}
int main()
[ //THIS SHOULD BE A } NOT A ]
std::cout << "In main\n";
DemonstrationFunction();
std::cout << "Back in main\n";
return 0;
}
and when I compile it says (all errors)
listing26.cpp:4: error: ISO C++ forbids declaration of `DemonstrationFunction'
with no type
listing26.cpp:10: error: parsningsfel before `;' token
listing26.cpp:11: error: ISO C++ forbids declaration of `DemonstrationFunction'
with no type
listing26.cpp:12: error: syntaxfel before `<<' token
I guess the errors after the first depend on the first one.
How do I call a simple function like that DemonstrationFunction?
See the above notes in the code. Corrected code is below,
#include <iostream>
void DemonstrationFunction()
{
std::cout << "In Demonstration Function\n";
}
int main()
{
std::cout << "In main\n";
DemonstrationFunction();
std::cout << "Back in main\n";
return 0;
}
Not using a { is a normal PEBKAC error, but function declarations in C++ must have a return type:
Lots of luck!!
agatonsax
07-28-2005, 08:06 PM
Whops!
Thanks for finding that little error. I showed my error for 2 other people but it wasn't a copy like this but written from memory, I guess I got it right instead of duplicating..... So they couldn't find any errors.
I tried with int and void and both worked now when replacing the [
Thanks guys! I don't know what I would have done without your help.
I guess return thingys will be in coming lessons :)
What does PEBKAC stand for?
celticgeek
07-28-2005, 08:50 PM
PEBKAC is an acronym that stands for "Problem Exists Between Keyboard And Chair". It usually means you have done something wrong, for example, a typing error. I personally only made about six or eight typing errors in answering your post. I edited it twice and it still had typos in it.
Some days are like that.
Good luck with C++!
agatonsax
07-29-2005, 07:02 AM
lol, good acronym. Will remember it.
Thanks.