🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: be.comp.programming
Article #15342

Re: ANSI strings toewijzen aan een string object in C++.

#15342
From: Andy Georges
Date: Mon, 16 Oct 2000 00:00
72 lines
2646 bytes
Hello,

> Ik heb een tabel met ANSI C strings en die strings zou ik graag willen
> toewijzen een tabel van string objecten in C++ (onder Linux).

uw voorbeeld, maar dan wat uitgebreid:

#include <string>

int main(void) {
  
  char *tabel[] = {"string1", "nog eentje", "dit is de laatste"};
  string* tabel2;

  tabel2 = new string[3];

  for (int i = 0; i < 3; i++)
    tabel2[i] = tabel[i];

  for(int i=0;i<3;++i) {
    cout << "tabel2[" << i << "].size() = " << tabel2[i].size() << endl;
    cout << "tabel2[" << i << "] = " << tabel2[i] << endl;
  }

  return 0;
}

en de output:

twizors:~/Bucht$g++ string.cpp 
twizors:~/Bucht$./a.out
tabel2[0].size() = 7
tabel2[0] = string1
tabel2[1].size() = 10
tabel2[1] = nog eentje
tabel2[2].size() = 17
tabel2[2] = dit is de laatste
twizors:~/Bucht$


> De declaraties en de creatie van de tabel (new string[]) gaan perfect. Maar
> de assignment in de for lus wil niet werken (tijdens run-time, compileren
> gaat zonder problemen). In Linux krijg ik telkens op dat statement:
> "Segmentation fault".

ik doe hier maar een gok, maar welke compiler gebruik je? ik heb egcs
2.95.2 gebruikt om dit voorbeeld te testen en alles gaat vlot. Misschien
is je STL niet de laatste versie. in elk geval, de volgende code vond ik
in de file /usr/include/g++/std/bastring.h (die wordt geincludeerd als
je string includeert):


  basic_string& operator= (const charT* s)
    { return assign (s); }

wat effectief doet wat jij ervan verwacht.


> Iemand enig idee wat ik verkeerd doe ? Of is het gewoon mijn systeem en moet
> ik gewoon mijn libraries opnieuw installeren ?

misschien es upgraden naar een nieuwere compiler?

mvg,
Andy
-- 
==========================================================================
Dept. of Applied Mathematics and Computer Science, University of Ghent
Krijgslaan 281 - S9, B - 9000 Ghent, Belgium
Phone: +32-9-264.47.66, Fax: +32-9-264.49.95
E-mail: andy.georges@rug.ac.be 
==========================================================================
Technical Details
Message-ID: <39EAC142.453E651C@rug.ac.be>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <8scsaa$e9r$1@news1.skynet.be>

Show Full Headers
Complete Article Headers
X-Google-Language: DUTCH,ASCII-7-bit
X-Google-Thread: fcea4,6025f5a818d56e9c
X-Google-Attributes: gidfcea4,public
From: Andy Georges <andy.georges_nospam@rug.ac.be>
Subject: Re: ANSI strings toewijzen aan een string object in C++.
Date: 2000/10/16
Message-ID: <39EAC142.453E651C@rug.ac.be>
X-Deja-AN: 681965198
Content-Transfer-Encoding: 7bit
References: <8scsaa$e9r$1@news1.skynet.be>
X-Accept-Language: en
Content-Type: text/plain; charset=us-ascii
Organization: University of Ghent, Belgium
Mime-Version: 1.0
Newsgroups: be.comp.programming