learning c++
Ok so bare with me please as im quiet nex to c++ and linux. I borwed a book to help me learn C++ the book starts out with an example program to get you started it is:
Code:
#include <iostream> Code:
gcc first.c So I then found the example c++ program called hellow Code:
#include <stdio.h> Code:
hello.c:7:2: warning: no newline at end of file So maybe someone could let me know what is wrong with the above program? i just want to nkow before I start getting into this book and start having more problems running there example programs. Sorry if this doesnt make alot of sence, i tried to state it the best way i could. Thank you again to anyone who may help or guide me in this issue. |
ok, you'reusing C++ so try compiling with g++
that should solve your problem, also make the file .cpp in steadof .c that way g++ and gcc will know it's a C++ file. Jule |
The first code sample you posted was C++, while the second code sample you posted was C. Try readjusting your compiler commands appropriately and report back with the results.
|
Hey thanks for the response dizzutch and Dodgeram01.
Ok so I did somemore reading and yes the top one is C++ the other is C, so since im trying to learn C++ ill stick with the first one mentioned above. So I renamed the file first.cpp from first.c I then ran at console g++ first.cpp ( I ran --help first but wasnt really sure what to use since im a new at this so just ran g++ first.cpp) And I recieved the same above errors : Code:
g++ first.cpp But thanks for your help Im willing to try anything to learn what it is im doiing wrong. Thank you once again for your time!! Axiom |
std:cout
Should be: std::cout |
Ok so i found my first mistake:
in the code i typed std:cout should have been std::cout So it worked in g++ it created the a.out file. But a friend of mine ran his with gcc first.cpp and his worked? Mine when ran through gcc gives me this: Code:
gcc first.cpp tput in g++ but still a bit perplexed as why i cant do it in gcc and a friend can? we both are running suse9.2 with all the current updates. so anymore help is grealtly welcomed. Thankyou Axiom thanxs deiussum i just saw your post |
Both gcc and g++ are front-ends for the GNU compiler collection. AFAIK g++ just sets some options in terms of lnking in the C++ standard libraries. I think if you name your program file (i.e. with a C++ file extension such as .C or .cxx, gcc will pick up on the fact that it's C++ and linkthose in too. This is probably what your friend did. If you try to compile and link C++ without linking in the C++ standard libs, as you saw, bad things can happen.
What version of gcc do you have? I think more recent ones willrecognize cpp as a valid C++ extension. [Edit: I just tried this (I don't do C++ very much) and I couldn't make it work without using g++ -- are you sure your friend was able to? maybe he explicitly linked inthe C++ libs.] |
I'm also learning c++, trying to come to grips with arrays and pointers at the moment (stressing). I believe the correct way of writing the first program using the new C++ standards is,
#include <iostream> using namespace std; int main (void) { //write to the screen cout << "My first c++ program"; return 0; } Then using g++ do, $g++ -o programname programname.cpp |
Keep in mind that you don't want to use using namespace std; in production code. Instead, prepend the 'std' scope qualifier to functions.
#include <iostream> int main (void) { // you could actually do this: // using std::cout; // cout << "Moo\n"; // or call the function directly (typically a better idea) std::cout << "My first c++ program" << std::endl; return 0; } |
Why isn't using namespace std; not used in production code? Still a newbie to programming, so I'm very curious.
|
I said it wasn't a good idea to use it in production code, that isn't to say programmers (professional or not) do it anyway.
The reason is, you're effectively importing the entire namespace. It defeats the purpose of having the namespace for organization. You may run into an error down the line with a function name clashing with something in std. The best thing to do is to either specify the full name (scope included) of the function. Aside from function name clashing, there's the possibility of unnecessary overhead. This isn't always the case, but, it's something to consider. In my opinion, leaving the namespace std alone often makes code look better too. "Where is that function? Oh, it's in std." |
I get what you mean now, thanks for the extra detail:)
|
Thanks again for all the input into this question!!
Well if you noticed in my first post above I did originally use the .C for my file extension. ANd still could not get it to work in gcc (gcc version: 3.3.4) And I know the code may not be written "perfectly" but that source was taken line from line from a book that I am trying to use to teach myself with. So I was a bit foggy as to why it wouldnt work with gcc and would with g++. So as you can see I really dont know much about c++, basically I was just reading through my book "Wiley's Tech yourself C++ 7th Edition" (got this book used for dirt cheap so I thought it would be a good score) and saw that first snippet of code and thought I would try it just to see if it would work. I have seen some books that are specifically titled "c++ for Linux" and such, but I thought since c++ wasnt platform dependent that I could use whatever book to help teach me the foundations of programming in C++. Am I wrong in this statement? Or better yet is there a book or books someone can recommend for learning c++? Well thanks again for all your help here, I still have lots more to learn so I sure I will be back to pick the minds of the linuxquestions community!! Thanks again, help is very much appreciated over here :study: Axiom |
The C/C++ language itself isn't platform dependent, but there are platform specific APIs... For instance, in Windows you have the Win32 API, which is specific to Windows. For Linux/UNIX you have a number of other APIs that are specific to UNIX based platforms.
However, even though those APIs are fairly platform specific, you can sometimes get a port of them. For instance, I believe that Winelib is basically a port of the Win32 API functions to Linux, and for Windows, you can get most of the Linux/UNIX APIs using a tool like Cygwin... If you stick to those functions of C/C++ that are defined by the ANSI/ISO standard, your code should stay pretty portable. Once you are ready to move on to other APIs, you should better understand C/C++ and be able to make choices of the APIs you want to use. |
I've found the O'Reilly book Practical C++ Programming a good book, I'm also trying to learn C++. I don't have time to study it but, I like this book.
|
All times are GMT -5. The time now is 07:42 PM. |