LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   learning c++ (https://www.linuxquestions.org/questions/programming-9/learning-c-272059/)

g2axiom 12-30-2004 05:09 PM

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>

int main()
{
        //write to the screen
        std:cout << "My first c++ program";
        return 0;
}

so i saved the file as frst.c and ran it in gcc. I recieved the following error:
Code:

gcc first.c
first.c:1:20: iostream: No such file or directory
first.c: In function `main':
first.c:6: error: `cout' undeclared (first use in this function)
first.c:6: error: (Each undeclared identifier is reported only once
first.c:6: error: for each function it appears in.)

My question inst so much that I got an error, but rather is it possbile to have c++ examples that only work in windows? As i thought c++ was not platform dependent.

So I then found the example c++ program called hellow
Code:

#include <stdio.h>
int main()
 {
  (void)printf("Hello, World!\n");
  return 0; /* Just to be nice */

}

Now this one worked with no other issue than
Code:

hello.c:7:2: warning: no newline at end of file
this one produced the output file a.out and I was able to run it and receive the desired output message from the program in console.

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.

dizzutch 12-30-2004 05:11 PM

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

Dodgeram01 12-30-2004 05:30 PM

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.

g2axiom 12-30-2004 07:26 PM

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
first.cpp: In function `int main()':
first.cpp:6: error: use of namespace `std' as expression
first.cpp:6: error: syntax error before `:' token

So im not sure if this is somethign I am doing wrong or am I missing some sort of library?
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

deiussum 12-30-2004 07:34 PM

std:cout

Should be:

std::cout

g2axiom 12-30-2004 07:43 PM

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
/tmp/ccXsPy2h.o(.text+0x25): In function `main':
: undefined reference to `std::cout'
/tmp/ccXsPy2h.o(.text+0x2a): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccXsPy2h.o(.text+0x56): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `std::ios_base::Init::Init[in-charge]()'
/tmp/ccXsPy2h.o(.text+0x85): In function `__tcf_0':
: undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/tmp/ccXsPy2h.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status

So i guess I am happy that it worked and created the ou

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

btmiller 12-30-2004 07:57 PM

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.]

reddazz 12-30-2004 08:16 PM

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

fr0zen 12-30-2004 11:26 PM

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;
}

reddazz 12-31-2004 02:00 AM

Why isn't using namespace std; not used in production code? Still a newbie to programming, so I'm very curious.

fr0zen 12-31-2004 05:22 AM

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."

reddazz 12-31-2004 05:46 AM

I get what you mean now, thanks for the extra detail:)

g2axiom 12-31-2004 12:05 PM

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

deiussum 12-31-2004 01:07 PM

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.

microsoft/linux 12-31-2004 07:57 PM

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.