Article View: comp.lang.c++.leda
Article #126Re: Help me please...
From: maite
Date: Tue, 10 Oct 1995 11:10
Date: Tue, 10 Oct 1995 11:10
134 lines
3724 bytes
3724 bytes
wu@inf.fu-berlin.de (Wei-ju Wu) wrote:
>
> In article <44kfkv$mf5@taco.cc.ncsu.edu>,
> khpark@eos.ncsu.edu (Ki Hong Park) writes:
> >
> >I was trying to write a simple program and ran into a problem that
> >I didn't know why the compiler was giving error.
> >I am using AT&T C++ 3.0 compiler and am working on DEC stations
> >with Unix on it.
> >
> >Here is my problem.
> >I have a input file called INPUT which contains some numbers.
> >ex. 5 10 15 20
> >The program is suppose to read first input number and create an array
> >with that length.
> >ex. if first input = 5, then length = input and Array[length].
> >
> >This is what I have.
> (...)
> > int a;
> > int b;
> > input >> a;
> > input >> b;
> > int ArrayA[a]; // line 12
> > int ArrayB[b]; // line 13
> > return 0;
> >When I try to compile this I get an error like this:
> >% CC test.C // compile command at the prompt
> >CC test.C:
> >"test.C", line 12: error: cannot evaluate constant
> >"test.C", line 13: error: cannot evaluate constant
> >2 errors
> >
> >
> >What is wrong with this simple program?
> >What can I do so that I can create arrays with the input from the file?
> >
> >Please help me and reply to: khpark@eos.ncsu.edu
> >Thanks.
>
>
> You can't initialize the arrays using variables, at least, not this way.
> My suggestion is, if you really want to allocate them with variables
> (Why not using constants ?), do it dynamically, or use the LEDA class
> "array" which is in <LEDA/array.h>
> If you have this then you can do something semilar to:
>
> #include <iostream.h>
> #include <LEDA/array.h>
>
> int main() {
> int a,b,i;
>
> cin >> a; cin >> b; // replace with file input
>
> array<int> ArrayA(a);
> array<int> ArrayB(b);
> // Read the values for the arrays
> }
>
> I tried this and it worked on our university system without problems
> using the GNU-C++ compiler
>
> If you don't have LEDA (why do you post it in this group then ?)
> you might also use your C++ compiler's own array class or
> make your ArrayB/ArrayA pointers to ints and
> try:
> ArrayA=new int[a];
> This is a less elegant way, but anyway, it works.
>
> Ciao, Wei-ju
>
> ------------------------
> Wei-ju Wu
> Fachbereich Informatik
> Freie Universitaet Berlin
> wu@inf.fu-berlin.de
>
Hello, I´ve got a similar problem.
I want to read an array structure from a file in the same way,
the first number of the line will be the size of the array.
I use the leda type array, but my problem is that I use a class
and I don´t know how to implement the defenition of the " >> "
and "<<" operators.
Simplifying, my class is like that:
class ruta {
int number_of_nodes;
array<int> nodes; // array with empty index set
public:
ruta () { --- }
ruta (const ruta& r) { ---}
friend istream& operator >> (istream& is, ruta& r)
{
---
I suppose I need to read the size (first number) and resize
the array in order to read it.
¿?
}
friend ostream& operator << (ostream& os, const ruta& r)
{
¿?
}
friend int compare (const ruta& r, const ruta& r)
{
---
}
}
The data input file would be something like:
3; 1 2 3
2; 2 4
2; 4 1
3; 1 4 2
..
Any help will be appreciated.
Thanks
Luis Bernaldez
luis@mat.upc.es
Technical Details
Message-ID:
<45dkb7$d0v@diable.upc.es>Path:
arc1-ca.pugleaf.net!diable.upc.es!usenetReferences:
<44kfkv$mf5@taco.cc.ncsu.edu> <44oh8v$svi@fu-berlin.de>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!diable.upc.es!usenet From: maite <luis@mat.upc.es> Newsgroups: comp.lang.c++.leda Subject: Re: Help me please... Date: 10 Oct 1995 11:10:31 GMT Organization: Universitat Politecnica de Catalunya Distribution: world Message-ID: <45dkb7$d0v@diable.upc.es> References: <44kfkv$mf5@taco.cc.ncsu.edu> <44oh8v$svi@fu-berlin.de> NNTP-Posting-Host: 147.83.39.180