View Full Version : new to c++
mwanafunzi
February 23rd, 2006, 12:32 PM
Hi all,
This is just to introduce myself on this forum. I am new to programming and I was hoping to get some help. Before I start of with any questions I just wanted make sure that it is ok for me to post my programming questions here. They are very basic questions (as I am a programming virgin). The language that I am learning is c++.
abhaysahai
February 23rd, 2006, 12:38 PM
Go ahead and shoot.
mwanafunzi
February 23rd, 2006, 03:37 PM
I had a number of question, but i only remeber one....
There was an example on what functions were that I was reading up on, I tried to write it up and compile it. But I got an error.
The cause of the error was that I said
void main() instead of int main()
The thing is that in the example they use void main(). My question is why did I get this error. Here is the code, and the error message.
#include <iostream.h>
void newLine()
{
cout << endl;
}
void main()
{
cout << "first line" << endl;
newLine();
cout << "second line" <<endl;
}
The error message
exFunc.cpp:7: error: ‘::main’ must return ‘int’
hod139
February 23rd, 2006, 03:52 PM
The quick answer is that void main() is not legal in C++. It is legal (though bad practice) in C, but the C++ standard requires main to return an int.
Edit: I should also add that you are including a deprecated header. you should be including <iostream> not <iostream.h>
Lastly, this code won't compile even if you fix the return type of main. The cout and endl are defined in the std namespace, and you need to tell your program that. The easiest way for now is to add
using namespace std; to the top of the program. I have attached a fixed version of your sample.
#include <iostream>
using namespace std;
void newLine()
{
cout << endl;
}
int main(void)
{
cout << "first line" << endl;
newLine();
cout << "second line" <<endl;
return 0;
}
abhaysahai
February 23rd, 2006, 04:04 PM
First of all thanks for asking this. I knew that void main () is not supported by c++, but to reply i had to google.
The various answers are
1) As per C99 standards the syntax is int main (...), so it is illegal to use void main....
2) Another good explanation of this
In C and C++, the main() function _has_ to return an int, which will
be used by the shell which run the program. However, with C++, the
return statement isn't mandatory anymore, and a main() function with a
missing return statement will silently return 0.
Guys any more official commens on int main vs void main ????
# if u want to program in C++ use #include <iostream>
instead of #include <iostream.h>
Pay attention to warnings.
Regards,
Abhay
mwanafunzi
February 23rd, 2006, 04:12 PM
thanks for the quick reply,
I did get an message saying that iostream.h is deprecated. I tried using iostream but I got and error message saying
exFunc.cpp: In function ‘void printTime()’:
exFunc.cpp:6: error: ‘cout’ was not declared in this scope
exFunc.cpp:6: error: ‘endl’ was not declared in this scope
exFunc.cpp: In function ‘int main()’:
exFunc.cpp:11: error: ‘cout’ was not declared in this scope
exFunc.cpp:11: error: ‘endl’ was not declared in this scope
So, I kept using iostream.h.
Anyway, it seems that using namespace std; has solved that problem.
My next question is why is it that with iostream.h there is nothing else to include where as with iostream we have to add using namespace std?
thanks
hod139
February 23rd, 2006, 04:18 PM
.
2) Another good explanation of this
In C and C++, the main() function _has_ to return an int, which will
be used by the shell which run the program. However, with C++, the
return statement isn't mandatory anymore, and a main() function with a
missing return statement will silently return 0.
This isn't quite true. In C, main does not have to return an int. It can return anything. See http://homepages.tesco.net/J.deBoynePollard/FGA/legality-of-void-main.html for an explanation.
abhaysahai
February 23rd, 2006, 04:19 PM
The quick answer is that void main() is not legal in C++. It is legal (though bad practice) in C, but the C++ standard requires main to return an int.
Is it legal to use void main () in C ??? I think that in C99 it is illegal, though it is not implemented strictly in gcc, maybe for backward compatibility.
Please correct me if I am wrong.
hod139
February 23rd, 2006, 04:21 PM
Is it legal to use void main () in C ??? I think that in C99 it is illegal, though it is not implemented strictly in gcc, maybe for backward compatibility.
Please correct me if I am wrong.
See my above post and link (http://homepages.tesco.net/J.deBoynePollard/FGA/legality-of-void-main.html). Using gcc you will get warnings if you don't use int for the return type, but anything is allowed and it will compile. C++ will throw an error.
hod139
February 23rd, 2006, 04:31 PM
My next question is why is it that with iostream.h there is nothing else to include where as with iostream we have to add using namespace std?
1. The iostream.h header is outdated and does not include the namespace protection. For a quick introduction to namespaces see http://www.glenmccl.com/ns_comp.htm and http://www.winterdom.com/dev/cpp/nspaces.html (found by google).
2. You didn't have to add using namespace std. For this simple example that was the easiest thing to do. Another way you could have written your code is this:
#include <iostream>
void newLine()
{
std::cout << std::endl;
}
int main(void)
{
std::cout << "first line" << std::endl;
newLine();
std::cout << "second line" << std::endl;
return 0;
}
which is more explicit.
hod139
February 23rd, 2006, 04:34 PM
I should also add that this sub-thread doesn't get nearly as many viewers. You might be better off posting future questions to the main Programming Talk thread.
mwanafunzi
February 23rd, 2006, 04:40 PM
Edit: Just as an example, what programs in ubuntu have been written in C++?
mwanafunzi
February 23rd, 2006, 04:45 PM
Thanks for the help guys. I will get to reading the links. I am sure it will take me a while before my next question (while I digest all the info that has been provided.
Again, thanks.
bored2k
February 23rd, 2006, 07:09 PM
Edit: Just as an example, what programs in ubuntu have been written in C++?
Gnome is mostly based on C. KDE is mostly based on C++. That's one of the main differences in both of them.
abhaysahai
February 24th, 2006, 12:16 PM
w
abhaysahai
February 24th, 2006, 12:16 PM
Gnome is mostly based on C. KDE is mostly based on C++. That's one of the main differences in both of them.
This explains the better memory management in KDE.
I mean less memory leaks.
hod139
February 24th, 2006, 02:53 PM
This explains the better memory management in KDE.
I mean less memory leaks.
Care to back up these claims?
mwanafunzi
February 25th, 2006, 06:21 PM
I am sure that this is something that I dont have to worry about right now. Why (specifically) are memory leaks a bad thing :-k ?
thumper
February 27th, 2006, 10:51 PM
'cause you run out of memory :)
pharcyde
March 5th, 2006, 02:22 AM
Use STD C++ libraries without .h on the name (i.e. <iostream> instead of <iostream.h>) Most c libraries are include using the .h and C++ libraries usually follow the convention of not having the .h.
www.cppreference.com is a great reference on STD C++ library implementations.
pharcyde
March 5th, 2006, 02:33 AM
I am sure that this is something that I dont have to worry about right now. Why (specifically) are memory leaks a bad thing :-k ?
You run out of memory because the space is allocated and then never collected after the program has run. A good example of this can be seen below.
int main(viod)
{
int *i = new int;
int *j = new int;
int *j = new int;
return 0;
}
The three integer pointers will point to dynamically allocated space requested from the O/S. Once the program is done executing the memory may or may not be reclaimed by the O/S (depends on some things). This is not particularly bad in this program because u are losing around 32bits per allocation in space everytime this "new int" is called. This type of programming becomes bad when you have very large dynamic data structures that never get collected. <- This happens often in some large programs, especially in Windows (Ever remembering having to restart Windows after running large applications?). You can use encapsulating pointer types such as http://www.cppreference.com/cppmisc/auto_ptr.html or any of the pointers listed in http://www.boost.org/libs/smart_ptr/smart_ptr.htm. These type of pointers have automatic memory management once they lose their intended scope or when the memory is no longer being pointed to.
zkissane
March 14th, 2006, 05:19 PM
Later on, you may also design some of your own classes that do things like open databases, files, mutexes, GDI objects (in Windows) and/or sockets. The errors that pharcyde provided "just" leak memory because ints don't do anything really dramatic. Make a mistake like that with, say, a file manipulating class, and you risk corrupting the file, since you don't ever "clean it up" and the OS has a much lower chance of cleaning it up the way you want it.
fizmhd
March 21st, 2010, 04:56 PM
Use STD C++ libraries without .h on the name (i.e. <iostream> instead of <iostream.h>) Most c libraries are include using the .h and C++ libraries usually follow the convention of not having the .h.
www.cppreference.com (http://www.cppreference.com) is a great reference on STD C++ library implementations.
This one works ...
#include<iostream>
using namespace std;
int main()
{
cout<<"HI";
return 0;
}
Powered by vBulletin® Version 4.2.2 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.