Kopie von Array auf Array



  • Vielleicht kann mir hier ja jemand helfen:

    Folgende Aufgabe:
    Ein Array NodePot ist als zusaetzliches Member der Klasse zu definieren. Der Konstruktor, sowie die Funktion SetDimension sind entsprechend zu erweitern berechnete Lösung aus dem Loesungsarray im Gleichungssystem auf das Array NodePot uebertragen. Hierzu ist das index-array nindex zu verwenden.

    Die Klasse(Header):

    //file Netzwerk.h

    #ifndef NETZWERK_H
    #define NETZWERK_H

    #include "Array1D.h"
    #include "Array2D.h"
    #include "Glsys.h"

    #define NT_FLOAT 0
    #define NT_POTENTIAL 1
    #define NT_CURRENT 2

    class CNetzwerk {
    protected:
    //input daten
    long nnode, nvertex;
    CArray1D NodeTypes;//erlaubte Werte NT_xxx (s. oben)
    CArray1D NodeValues;//current or potential if applicable
    CArray2D Vertices;
    CArray1D Resistance;
    CGleichungssystem pGls;
    //abgeleitete Daten
    long nu;
    CArray1D uindex;//uindex(i)= nr der Gleichung zu Knoten i, -1=Knoten mit festem Pot
    CArray1D nindex;//nindex(i)= nr des Knotens zu Gleichung i
    CArray1D NodePot;
    public:
    CNetzwerk(void);
    ~CNetzwerk(void);
    void SetGleichungssystemPointer(CGleichungssystem pnGls);
    void ReadTestData(char
    filename);
    void ReadNetlist(char
    filename);
    void SetDimension(long nn, long nv);
    void SetupIndex(void);
    void SetupEqsys(void);
    void RestoreSolution(void);
    void OutputTestData(char* filename);
    void OutputData(char* filename);
    void RunTestProblem(void);
    };

    #endif

    Die zu bearbeitende Datei:

    // file Netzwerk.cpp

    #include "stdafx.h"
    void errbreak(char* message);
    #include "Netzwerk.h"
    #include <math.h>

    #ifdef _DEBUG
    #undef THIS_FILE
    static char BASED_CODE THIS_FILE[] = __FILE__;
    #define new DEBUG_NEW
    #endif

    CNetzwerk::CNetzwerk(void)
    {
    nnode=0;
    nvertex=0;
    nu=0;
    pGls=NULL;
    NodeTypes.SetType(A_INT);
    NodeValues.SetType(A_DOUBLE);
    Resistance.SetType(A_DOUBLE);
    Vertices.SetType(A_LONG);
    uindex.SetType(A_LONG);
    nindex.SetType(A_LONG);
    }

    CNetzwerk::~CNetzwerk(void)
    {
    }

    void CNetzwerk::SetDimension(long nn, long nv)
    {
    nnode=nn;
    nvertex=nv;
    NodeTypes.Allocate(nnode);
    NodeTypes.SetSize(nnode);
    NodeValues.Allocate(nnode);
    NodeValues.SetSize(nnode);
    uindex.Allocate(nnode);
    uindex.SetSize(nnode);
    Vertices.Allocate(nvertex*2);
    Vertices.SetSize(nvertex,2);
    Resistance.Allocate(nvertex);
    Resistance.SetSize(nvertex);
    }

    void CNetzwerk::RestoreSolution(void)
    {

    - hier soll der eigentliche Quelltext rein! -

    }

    Datei Array1D.h :

    //file Array1D.h

    #ifndef ARRAY1D_H
    #define ARRAY1D_H

    #define CHECK_BOUNDS
    #define DO_INLINE

    //#include "random.h"

    #define A_INT 1
    #define A_LONG 2
    #define A_FLOAT 3
    #define A_DOUBLE 4
    #define A_CHAR 5

    #define A_SORT_UP 0
    #define A_SORT_DOWN 1

    #define A_READABLE 0
    #define A_ASCII_IO 1
    #define A_BINARY_IO 2
    #define A_CODED_IO 3

    class CArray1D {
    protected:
    long max; //allocated length
    long n; // used length
    int type;
    int *vInt;
    long *vLong;
    float *vFloat;
    double *vDouble;
    char *vChar;
    virtual void SetZeroLength(void);
    virtual void GetElement(FILE *inp, long i);
    virtual void PutElement(FILE *out, long i);
    public:
    virtual void FullZero(long startindex=0);
    virtual void SetConst(double value);
    virtual void SetConst(long value);
    virtual int Allocate(long length);
    virtual int Free(void);
    CArray1D(void); //Construction,destruction
    CArray1D(int ntype);
    CArray1D(int ntype, long nmax);
    CArray1D(int ntype, long nn,long nmax);
    virtual ~CArray1D(void);
    int SetUpperBound(long nmax); //size/type management
    int SetSize(long nn);
    long GetUpperBound(void);
    long GetSize(void);
    void RemoveAt(long index);
    void SetType(int ntype);
    int GetType(void);
    #ifdef DO_INLINE //set/get elements
    inline void SetAt(long index, double nElem);
    inline void SetAt(long index, long nElem);
    inline double GetDoubleAt(long index);
    inline long GetLongAt(long index);
    #else
    void SetAt(long index, double nElem);
    void SetAt(long index, long nElem);
    double GetDoubleAt(long index);
    long GetLongAt(long index);
    #endif
    virtual void SetAt(long index, int nElem);
    void SetAt(long index, float nElem);
    void SetAt(long index, char nElem);
    void InsertAt(long index, int nElem);
    void InsertAt(long index, long nElem);
    void InsertAt(long index, float nElem);
    void InsertAt(long index, double nElem);
    void InsertAt(long index, char nElem);
    void Append(int nElem);
    void Append(long nElem);
    void Append(double nElem);
    virtual int GetAt(long index);
    int GetIntAt(long index);
    float GetFloatAt(long index);
    char GetCharAt(long index);
    void Increment(long index);
    void Increment(long index,long by_value);
    void Increment(long index,double by_value);
    void VectorSum(CArray1D& vec1, CArray1D& vec2); //vector operations
    double ScalarProduct(CArray1D& vec1);
    void SqrSort(int sorttype); // sorting/searching operations
    void SqrRank(int sorttype,CArray1D& Ranklist);
    void Count(void);
    void SortByRank(CArray1D& Ranklist);
    virtual void FileGet(FILE *inp); // File i/o
    virtual void FilePut(FILE *out);
    virtual void FileRead(char *filnam);
    virtual void FileWrite(char *filnam);
    CArray1D& operator = (CArray1D& source);
    virtual void Serialize(CArchive& ar);
    };

    #ifdef DO_INLINE

    inline void CArray1D::SetAt(long index, double nDouble)
    /*set element at certain value, leaves size unchanged*/
    {
    #ifdef CHECK_BOUNDS
    if(type!=A_DOUBLE) errbreak("CArray1D::SetAt(double): wrong type" ;
    if(index<0 || index>=n) errbreak("CArray1D::SetAt (double): index outta range" ;
    #endif
    vDouble[index]=nDouble;
    }

    inline void CArray1D::SetAt(long index, long nElem)
    /*set element at certain value, leaves size unchanged*/
    {
    #ifdef CHECK_BOUNDS
    if(type!=A_LONG) {
    errbreak("CArray1D::SetAt (long): wrong type" ;
    };
    if(index<0 || index>=n) {
    errbreak("CArray1D::SetAt (long): index outta range" ;
    };
    #endif
    vLong[index]=nElem;
    }

    inline double CArray1D::GetDoubleAt(long index)
    {
    #ifdef CHECK_BOUNDS
    if(type!=A_DOUBLE) errbreak("CArray1D::GetDoubleAt: wrong type" ;
    if(index<0 || index>=n) errbreak("CArray1D::GetDoubleAt: index outta range" ;
    #endif
    return vDouble[index];
    }

    inline long CArray1D::GetLongAt(long index)
    {
    #ifdef CHECK_BOUNDS
    if(type!=A_LONG) errbreak("CArray1D::GetAt: wrong type" ;
    if(index<0 || index>=n) {
    errbreak("CArray1D::GetLongAt: index outta range" ;
    };
    #endif
    return vLong[index];
    }

    #endif //ifdef DO_INLINE

    #endif//ifdef ARRAY1D_H

    Vielleicht kann mir ja wer helfen, wäre sehr dankbar 🙂



  • wo ist das problem?



  • naja ich bin nun kein c++-Held *gg* .... ich hab das so gemacht:

    // file Netzwerk.cpp

    #include "stdafx.h"
    void errbreak(char* message);
    #include "Netzwerk.h"
    #include <math.h>

    #ifdef _DEBUG
    #undef THIS_FILE
    static char BASED_CODE THIS_FILE[] = __FILE__;
    #define new DEBUG_NEW
    #endif

    CNetzwerk::CNetzwerk(void)
    {
    nnode=0;
    nvertex=0;
    nu=0;
    pGls=NULL;
    NodeTypes.SetType(A_INT);
    NodeValues.SetType(A_DOUBLE);
    Resistance.SetType(A_DOUBLE);
    Vertices.SetType(A_LONG);
    uindex.SetType(A_LONG);
    nindex.SetType(A_LONG);
    NodePot.SetType(A_LONG); /* neu */
    }

    CNetzwerk::~CNetzwerk(void)
    {
    }

    void CNetzwerk::SetDimension(long nn, long nv)
    {
    nnode=nn;
    nvertex=nv;
    NodeTypes.Allocate(nnode);
    NodeTypes.SetSize(nnode);
    NodeValues.Allocate(nnode);
    NodeValues.SetSize(nnode);
    uindex.Allocate(nnode);
    uindex.SetSize(nnode);
    Vertices.Allocate(nvertex2);
    Vertices.SetSize(nvertex,2);
    Resistance.Allocate(nvertex);
    Resistance.SetSize(nvertex);
    NodePot.Allocate(nnode); /
    neu /
    NodePot.SetSize(nnode); /
    neu */
    }

    void CNetzwerk::RestoreSolution(void)
    {
    long i;

    nindex.Allocate(nu);
    nindex.SetSize(nu);
    NodePot.Allocate(nu);
    NodePot.SetSize(nu);

    for(i=0;i<nu;i++)
    NodePot.GetLongAt(i)=nindex.GetAt(i);
    }

    Aber dann kommt Fehlermeldung für die letzte Zeile: error C2106: '=' : left operand must be l-value

    😞



  • Hab mir deinen Code jetzt nicht wirklich angeschaut, aber ersetze doch mal diese Zeile:

    inline long CArray1D::GetLongAt(long index)
    

    durch die:

    inline long& CArray1D::GetLongAt(long index)
    


  • ich mach das mal, aber eigentlich darf in der Datei Array1D.h nichts veränert werden! Nur da, wo ich auch was geändert hatte ...

    von einem Fehler auf 48 Fehler erhöht 😃 ...



  • itfatzke schrieb:

    von einem Fehler auf 48 Fehler erhöht 😃 ...

    Das soll erstmal einer überbieten 😃


  • Mod

    wie wärs denn mit pointern? also:

    inline long* CArray1D::GetLongAt(long index)
    {
      #ifdef CHECK_BOUNDS
      if(type!=A_LONG) errbreak("CArray1D::GetAt: wrong type");
      if(index<0 || index>=n) {
        errbreak("CArray1D::GetLongAt: index outta range");
      };
      #endif
      return &vLong[index];
    }
    
    .
    .
    .
    
    for(i=0;i<nu;i++)
      *(NodePot.GetLongAt(i))=nindex.GetAt(i);
    

    das ist nicht unbedingt guter stil, sollte aber funktionieren. das nächste mal benutz doch bitte code-tags 🙂



  • oh, von diesen Code-Tags wusst ich nix, sorry *gg* .....

    es geht darum:

    for(i=0;i<nu;i++)
      *(NodePot.GetLongAt(i))=nindex.GetAt(i);
    

    Das ist mit Sicherheit falsch, denn das hab ich selbst geschrieben 🙄 ... sicher geht das auch mit Zeigern, danke auch für den Tipp ... aber ich kann in dieser Array1D Datei nichts ändern, sondern nur hier:

    // file Netzwerk.cpp 
    
    #include "stdafx.h" 
    void errbreak(char* message); 
    #include "Netzwerk.h" 
    #include <math.h> 
    
    #ifdef _DEBUG 
    #undef THIS_FILE 
    static char BASED_CODE THIS_FILE[] = __FILE__; 
    #define new DEBUG_NEW 
    #endif 
    
    CNetzwerk::CNetzwerk(void) 
    { 
    nnode=0; 
    nvertex=0; 
    nu=0; 
    pGls=NULL; 
    NodeTypes.SetType(A_INT); 
    NodeValues.SetType(A_DOUBLE); 
    Resistance.SetType(A_DOUBLE); 
    Vertices.SetType(A_LONG); 
    uindex.SetType(A_LONG); 
    nindex.SetType(A_LONG); 
    NodePot.SetType(A_LONG); /* neu */ 
    } 
    
    CNetzwerk::~CNetzwerk(void) 
    { 
    } 
    
    void CNetzwerk::SetDimension(long nn, long nv) 
    { 
    nnode=nn; 
    nvertex=nv; 
    NodeTypes.Allocate(nnode); 
    NodeTypes.SetSize(nnode); 
    NodeValues.Allocate(nnode); 
    NodeValues.SetSize(nnode); 
    uindex.Allocate(nnode); 
    uindex.SetSize(nnode); 
    Vertices.Allocate(nvertex*2); 
    Vertices.SetSize(nvertex,2); 
    Resistance.Allocate(nvertex); 
    Resistance.SetSize(nvertex); 
    NodePot.Allocate(nnode); /* neu */ 
    NodePot.SetSize(nnode); /* neu */ 
    } 
    
    void CNetzwerk::RestoreSolution(void) 
    { 
    long i; 
    
    nindex.Allocate(nu); 
    nindex.SetSize(nu); 
    NodePot.Allocate(nu); 
    NodePot.SetSize(nu); 
    
    for(i=0;i<nu;i++) 
    NodePot.GetLongAt(i)=nindex.GetAt(i); 
    }
    

  • Mod

    dann wirst du es wohl so machen müssen:

    for(i=0;i<nu;i++)
      NodePot.SetAt( i, nindex.GetAt( i ) );
    


  • wow DANKE SCHÖN!!!! ...du hast mir damit wahnsinnig geholfen 😃

    👍 👍 😉


Anmelden zum Antworten