Welcome, Guest. Please login or register.

Author Topic: GCC:C++:STL:vector  (Read 6485 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline RodneyTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 1386
    • Show only replies by Rodney
    • http://donthaveone.com/
GCC:C++:STL:vector
« on: April 01, 2003, 01:25:10 PM »
I've recently installed GCC and all seemed to be working fine until now.

I get very strange, unreadable (to me) error messages when i try to create a vector.

I am kinda still learning this vector stuff, and in doing so i m creating this little program to aswere one of my questions about vectors. But now i have a more important question. What do these errors mean?

Here is the code:

#include
#include
using namespace std;

int main ()
{
    vector v;
/*    int a = 3;
    int b = 2;
    v.push_back(a);
    v.push_back(b);
    v[0] = 10;
    v[1] = 10;

    printf ("%d %d",a ,b);
   */
}

As you can see, i've commented out most of the code so that im pretty sure the top line of function main() is causing the problemos.

Incase you cant tell from the program, im trying to find out if when adding an element to a vector, does the vector simply copy the element past into push_back() and put it in the vector, or does it obtain the objects address and add the reference in the vector?

However, thats not really the guestion i want answered anymore (although if you know it, that'd be good) The main question is, am i doing something wrong with this code, or is this a problem of my gcc? And has this been reported before? if so, is there a fix etc?

Heres the errors the above code is producing!

Projects:test> gcc -o vex vex.cpp
/t/cc8816321.o: Undefined symbol ___nw__FUlPv referenced from text segment
/t/cc8816321.o: Undefined symbol endl(ostream &) referenced from text segment
/t/cc8816321.o: Undefined symbol _cerr referenced from text segment
/t/cc8816321.o: Undefined symbol ___ls__7ostreamPCc referenced from text segment
/t/cc8816321.o: Undefined symbol ___ls__7ostreamPFR7ostream_R7ostream referenced from text segment
We are not Humans having a spirital experiance
We are Spirits having a Human experiance.
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: GCC:C++:STL:vector
« Reply #1 on: April 01, 2003, 01:30:40 PM »
Youre getting a lot of C++ iostream references. The STL uses IO streams for IO in preference to the C IO style methods. My guess is that you're getting problems caused by unreferenced calls to IO stream code.

Try adding

#include

Also, you commented out everything that uses your vector - it may get optimised away...

Hope this helps.
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: GCC:C++:STL:vector
« Reply #2 on: April 01, 2003, 01:45:30 PM »
Quote

Rodney wrote:

Incase you cant tell from the program, im trying to find out if when adding an element to a vector, does the vector simply copy the element past into push_back() and put it in the vector, or does it obtain the objects address and add the reference in the vector?



Well, that depends on what you create a vector of. A template uses whatever T is given to it, be it a class, integer, float, int* you name it.
Internally, most vector implementations use something like a conventional array (the kind you get with operator new[]).

Getting back to our template stuff,  a vector is a vector of ints.

Similarly,

class Dummy {
  int a, b, c;
  float x, y, z;
...
}

vector is a vector of Dummy objects.

Assignment etc. would be done via a user-defined copy constructor if defined, or the default copy constructor if it isnt. For ints and other elemental types, copying is of the straightforward a = b; assignment type.

Beware of default copy construction in classes that have dynamically allocated data inside! You really need to take charge on how these things are copied yourself by providing a copy constructor.

Also, if you make the your copy constructor for your Dummy class private or protected you probably wouldn't be able to make a vector of it.

Moving on, if you want a vector of references, you need to do it something like this

vector

Or alternatively create a class which is a wrapper for a Dummy* and use a vector of that.

Anyhoo, hope this answers a your question a bit.
int p; // A
 

Offline RodneyTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 1386
    • Show only replies by Rodney
    • http://donthaveone.com/
Re: GCC:C++:STL:vector
« Reply #3 on: April 01, 2003, 02:05:57 PM »
Good suggestion with the however it didnt work. Infact, it cant even find the file. So i tried and well, got the errors.

After some buggering around i tried copying iostream.h in gnu/lib/g++include/ to iostream and then included got the same errors again.

I tried
#define iostream iostream.h
#include

So that hopfuly the #define would filter down into the vector include files, however, got an error at the '.' in the #define, but thats not a problem... Cause it was just a stab in the dark.

I've got version 2.7.0 of gcc i believe. Is this good enough by peoples experiance?
We are not Humans having a spirital experiance
We are Spirits having a Human experiance.
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: GCC:C++:STL:vector
« Reply #4 on: April 01, 2003, 02:18:53 PM »
Hmm,

Well the whole headername / headername.h thing is fairly recent in C++. According to the current standards, the .h part of the name is redundant (the compiler knows you're talking about a header). However, the .h should still work fine. Personally when I write my headers I stick to .hpp ;-)

As for your woes, I guess there may be a problem at the linker stage. After I posted the iostream thing I realised that it wouldn't really matter - the STL components that use IO stream would include it themselves. Since your main file isn't using any iostream stuff, things should be ok even though you didn't include it there.

My guess is that somehow your installation is broken - some .lib files may be missing or the compiler is somehow looking in the wron place for them.

Unfortunatley, I recently had to flatten my developer partition :-( so I need to go the the whole install/build hassle again...
int p; // A
 

Offline RodneyTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 1386
    • Show only replies by Rodney
    • http://donthaveone.com/
Re: GCC:C++:STL:vector
« Reply #5 on: April 01, 2003, 02:38:30 PM »
Well thanx for you help anyway. Ill have another look at it tomorrow, and possibly reinstall or  upgrade (if thats possible, this was the only installation of gcc i could find on aminet).

For now, im off to bed!
We are not Humans having a spirital experiance
We are Spirits having a Human experiance.
 

Offline trgse

  • Full Member
  • ***
  • Join Date: Jul 2002
  • Posts: 150
    • Show only replies by trgse
    • http://hem.fyristorg.com/TRG/
Re: GCC:C++:STL:vector
« Reply #6 on: April 01, 2003, 03:06:04 PM »
in my opinion you should install a newer version of GCC you are not even using egcs which means it's oooolllddd.

second thing (this aplies to anyone doing C++ with GCC) is to install stlport instead standard gnu stl implementation, stlport can be found at http://www.stlport.org
MacOS X rulez!

Quad Mac Rulez!
 

Offline RodneyTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 1386
    • Show only replies by Rodney
    • http://donthaveone.com/
Re: GCC:C++:STL:vector
« Reply #7 on: April 02, 2003, 04:39:41 AM »
Quote

trgse wrote:
in my opinion you should install a newer version of GCC you are not even using egcs which means it's oooolllddd.


Yer, this makes sence. A search on aminet returned some Warp UP stuff but nothing that would suggest it is a native C++ compiler for 68k AmigaOS.

it also returned egcs-src.lha . Which im guessing is the source of the compiler. Do you know where i can get the binaries from?

Quote

second thing (this aplies to anyone doing C++ with GCC) is to install stlport instead standard gnu stl implementation, stlport can be found at http://www.stlport.org


Whats the advantages of STLport and why would you recommended over the standard gnu implementation?
We are not Humans having a spirital experiance
We are Spirits having a Human experiance.
 

Offline CodeSmith

  • Sr. Member
  • ****
  • Join Date: Sep 2002
  • Posts: 499
    • Show only replies by CodeSmith
Re: GCC:C++:STL:vector
« Reply #8 on: April 02, 2003, 07:59:35 AM »
I haven't used g++ for quite a while now, but IIRC something you needed to do was, if you're using gcc to do the linking for you (eg gcc -omybin myobj1.o myobj2.o myobj3.o) is that you have to use the g++ command to do this, as gcc is the C compiler and g++, being the C++ compiler, does a bit more.  If you're using ld to link the final binary, you'd have to add "-lstdc++" or "-lstdcxx" to your command line (that's part of that extra stuff g++ does).  That library has all the stream code in it.

So, according to your example, you should be doing this:

Projects:test> g++ -o vex vex.cpp

(if g++ is not recognized as a command, try gxx)
 

Offline RodneyTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 1386
    • Show only replies by Rodney
    • http://donthaveone.com/
Re: GCC:C++:STL:vector
« Reply #9 on: April 02, 2003, 01:26:58 PM »
Quote

trgse wrote:
in my opinion you should install a newer version of GCC you are not even using egcs which means it's oooolllddd.


Yup :) good point. Problem. There seems to be now egcs on aminet except for some Warp Up stuff. There was a page which some guy had m68k hosted and targeted binaries there, and all the warpup stuff too, but non of the links work.

And then the ftp site of back2roots had 2 version of 68020 binaries for egcs... i think one was alpha 1.0.3 and the other one looked like a stable 1.1.2...
Is this the stuff i want? Or is there a more up to date version?


Quote

second thing (this aplies to anyone doing C++ with GCC) is to install stlport instead standard gnu stl implementation, stlport can be found at http://www.stlport.org


And this sounds a little weird. If i develop useing these libraries, will i still be able to port my stuff to other platforms and use the gnu STL's?

What advantages does this give me? I think at their site i saw something about optomisations. Do you have any other reasons for why their good to use?

Do i need to get an Amiga specific version of it?
We are not Humans having a spirital experiance
We are Spirits having a Human experiance.
 

Offline RodneyTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 1386
    • Show only replies by Rodney
    • http://donthaveone.com/
Re: GCC:C++:STL:vector
« Reply #10 on: April 02, 2003, 01:29:58 PM »
Quote

CodeSmith wrote:
I haven't used g++ for quite a while now, but IIRC something you needed to do was, if you're using gcc to do the linking for you (eg gcc -omybin myobj1.o myobj2.o myobj3.o) is that you have to use the g++ command to do this, as gcc is the C compiler and g++, being the C++ compiler, does a bit more.  If you're using ld to link the final binary, you'd have to add "-lstdc++" or "-lstdcxx" to your command line (that's part of that extra stuff g++ does).  That library has all the stream code in it.

So, according to your example, you should be doing this:

Projects:test> g++ -o vex vex.cpp

(if g++ is not recognized as a command, try gxx)


i dont know, but maybe what your talking about is Amiga specific. But on Unix, gcc does everything. infact, gcc is just a unified interface for g++ and the linker etc... gcc calls c++ if the code is in C++

eg, i was writing c++ in a file with the extension .c. gcc had errors... i changed the named to cpp and then it worked :). Gcc also supports the file extension .c++ for C++ code.

Anyway, thats what i thought! :)
We are not Humans having a spirital experiance
We are Spirits having a Human experiance.
 

Offline CodeSmith

  • Sr. Member
  • ****
  • Join Date: Sep 2002
  • Posts: 499
    • Show only replies by CodeSmith
Re: GCC:C++:STL:vector
« Reply #11 on: April 03, 2003, 04:36:45 AM »
Quote

Rodney wrote:

i dont know, but maybe what your talking about is Amiga specific. But on Unix, gcc does everything. infact, gcc is just a unified interface for g++ and the linker etc... gcc calls c++ if the code is in C++

eg, i was writing c++ in a file with the extension .c. gcc had errors... i changed the named to cpp and then it worked :). Gcc also supports the file extension .c++ for C++ code.

Anyway, thats what i thought! :)


Gcc will guess the language from the file extension, so you would have had errors with a cpp file called #?.c
I'm pretty sure the g++ thing is not an Amiga-only thing, because I first found out about it when I was doing some MS-DOS coding  :-D
If you don't have g++ on your platform, just add the "-lstdc++" switch to the last time you run gcc, or to the time you call ld, in your makefile.
 

Offline nyteschayde

  • VIP / Donor - Lifetime Member
  • Hero Member
  • *****
  • Join Date: Mar 2002
  • Posts: 643
    • Show only replies by nyteschayde
    • http://www.nyteshade.com
Re: GCC:C++:STL:vector
« Reply #12 on: April 03, 2003, 07:28:06 AM »
The code compiles for me. The output is 3 2. So the values stored in the vector are not stored by reference. As for your compilation errors... try installing a new version.
Senior MTS Software Engineer with PayPal
Amigas: A1200T 060/603e PPC • A1200T 060 • A4000D 040 • A3000 (x2) • A2000 Vamp/V2 • A1200 (x4) • A1000 (x3) • A600 Vamp/V1 • A500
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: GCC:C++:STL:vector
« Reply #13 on: April 03, 2003, 08:50:01 AM »
@Rodney

ftp://ftp.geekgadgets.org/pub/geekgadgets/amiga/m68k/alpha/gcc/

2.95.3

...there seems to be braind new 3.2.2 there too, but you'd better stick with 2.95.3 for now...
 

Offline RodneyTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 1386
    • Show only replies by Rodney
    • http://donthaveone.com/
Re: GCC:C++:STL:vector
« Reply #14 on: April 03, 2003, 09:42:09 AM »
Quote

nyteschayde wrote:
The code compiles for me. The output is 3 2. So the values stored in the vector are not stored by reference. As for your compilation errors... try installing a new version.


Thanx heaps :) but thats not what i wanted to hear :) That is, i was hopeing that'd be passed by reference. Not to worry.
We are not Humans having a spirital experiance
We are Spirits having a Human experiance.