Új hozzászólás Aktív témák

  • papa019

    senior tag

    Sziasztok.
    Van egy hatalmas problémám. :S

    Stringekből (osztály) álló vektor osztályt kellett készítenünk.
    Minden megy szuperül egy dolgot kivéve:

    //using namespace std;
    ostream & operator << (ostream& os, const Vector &v)
    {
    for(unsigned int i=0;i<v.elementNum;i++)
    {
    os<<' '<<v.at(i);
    }
    return os;
    }

    Így olyan hibákat dob ki a rendszer, hogy:

    1>d:\egyetem\2. félév\programozás alapjai 2\khf\7.hazi\7\7\vector.cpp(124) : error C2143: syntax error : missing ';' before '&'
    1>d:\egyetem\2. félév\programozás alapjai 2\khf\7.hazi\7\7\vector.cpp(124) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>d:\egyetem\2. félév\programozás alapjai 2\khf\7.hazi\7\7\vector.cpp(124) : error C2065: 'os' : undeclared identifier
    1>d:\egyetem\2. félév\programozás alapjai 2\khf\7.hazi\7\7\vector.cpp(124) : error C2059: syntax error : 'const'
    1>d:\egyetem\2. félév\programozás alapjai 2\khf\7.hazi\7\7\vector.cpp(125) : error C2143: syntax error : missing ';' before '{'
    1>d:\egyetem\2. félév\programozás alapjai 2\khf\7.hazi\7\7\vector.cpp(125) : error C2447: '{' : missing function header (old-style formal list?)

    Ha a namespace nincs kikommentezve, akkor pedig egy nagyon hosszú hibasort, mely az ostream-re mutat...

    A forráskódok:

    Vector.cpp:
    #include <limits.h>
    #include <assert.h>
    #include <iostream>
    #include "Vector.h"
    #include "string.h"

    using namespace HomeMadeString;

    Vector::Vector(const Vector& theOther)
    {
    pData=NULL;
    *this=theOther;
    }

    void Vector::clear()
    {
    elementNum=0;
    delete [] pData;
    }


    void Vector::erase(unsigned int position)
    {
    assert(position<elementNum);

    if(elementNum==1)
    {
    delete []pData;
    pData=NULL;
    elementNum=0;
    return;
    }

    elementNum--;
    String* pTemp=new String[elementNum];

    for(unsigned int i=0, j=0;i<elementNum+1;i++,j++)
    {
    if(i==position)
    {
    j--;
    }
    else
    {
    pTemp[j]=pData[i];
    }
    }
    delete[] pData;
    pData=pTemp;
    }

    String& Vector::at(unsigned int position)
    {
    assert(position<elementNum);
    return pData[position];
    }

    const String& Vector::at(unsigned int position)const
    {
    assert(position<elementNum);
    return pData[position];
    }

    bool Vector::insert(unsigned int position, String element)
    {
    if(elementNum==UINT_MAX)
    {
    return false;
    }
    if(position<elementNum)
    {
    String* pTemp=new String[elementNum+1];
    for(unsigned int i=0,j=0;i<elementNum;i++,j++){
    if(j==position){
    pTemp[i]=element;
    j--;
    }
    else pTemp[i]=pData[j];
    }
    delete[] pData;
    pData=pTemp;
    elementNum++;
    }
    else{
    String* pTemp=new String[position+1];
    for(unsigned int i=0;i<=position;i++){
    if(i<elementNum)pTemp[i]=pData[i];
    else{
    if(i==position)pTemp[i]=element;
    else pTemp[i]=0;
    }
    }
    delete[] pData;
    pData=pTemp;
    elementNum=position+1;
    }
    return true;
    }
    String & Vector::operator [](unsigned int position)
    {
    return at(position); // Miért nem return pData[position]; ???
    }
    const String & Vector::operator [](unsigned int position)const
    {
    return at(position); // Miért nem return pData[position]; ???
    }

    const Vector& Vector::operator= (const Vector & theOther)
    {
    assert(this!=&theOther);
    delete[] pData;
    elementNum=theOther.elementNum;
    if(elementNum==0) pData=NULL;
    else {
    pData=new String[elementNum];
    for(unsigned int i=0;i<elementNum;i++) pData[i]=theOther.pData[i];
    }
    return *this;


    }
    using namespace std;
    ostream & operator << (ostream& os, const Vector &v)
    {
    for(unsigned int i=0;i<v.elementNum;i++)
    {
    os<<' '<<v.at(i);
    }
    return os;
    }

    Vector.h:
    #ifndef VECTOR_H
    #define VECTOR_H

    #include <iostream>
    #include "string.h"

    // Egy dinamikus tömb osztály
    using namespace HomeMadeString;
    class Vector
    {
    // A tömb mérete
    unsigned int elementNum;
    // Az adatokra mutató pointer
    String *pData;

    // Diagnosztikai célú globális kiiratóoperátor
    friend std::ostream & operator << (std::ostream& os,const Vector& v);

    public:
    // Konstruktorok, destruktor
    Vector() {elementNum=0;pData=NULL;}
    Vector(const Vector& theOther);
    ~Vector() {delete[]pData;}

    // Visszatér a tömb méretével.
    int size()const{return elementNum;}
    // Törli a tömböt (ez konzisztens állapotba is hozza a tömböt, nem csak a dinamikus adatterületet szabadítja fel)
    void clear();
    // Törli a megadott indexu elemet. A 0 és size()-1 közötti indexek érvényesek.
    void erase(unsigned int position);
    // Visszatér a megadott indexu elemmel, amely módosítható is egyben.
    // A 0 és size()-1 közötti indexek érvényesek.
    String& at(unsigned int position);
    // Visszatér a megadott indexu elemmel, amely csak olvasható.
    // A 0 és size()-1 közötti indexek érvényesek. Az int típus esetén nem kellene
    // const referencia, de saját típus esetén igen, lásd házi feladat.
    const String& at(unsigned int position)const;
    // Beszúr egy elemet a megadott indexu helyre.
    // Ha az index nagyobb, mint a tömb mérete, megnöveli a tömb méretét,
    // és a szükséges új helyeket nullákkal tölti fel.
    bool insert(unsigned int position, String element);

    // Operator=
    const Vector& operator= (const Vector & theOther);

    // Két operator[]. Az at() tagfüggvény operator formában is.
    String & operator [](unsigned int position);
    const String & operator [](unsigned int position)const;
    };

    // Diagnosztikai célú kiiratás
    std::ostream & operator << (std::ostream& os, Vector& v);
    #endif /*VECTOR_H */

    String.h:
    #ifndef STRING_H
    #define STRING_H
    #include <iostream> // A coutnak

    namespace HomeMadeString
    {
    class String
    {
    // A karakterek aktuális száma:
    int elementsNum;

    // A karaktereket tartalmazó memóriaterületre mutató pointer:
    char *pData;
    public:
    // Argumentum nélküli konstruktor:
    String();

    // Másoló konstruktor:
    String(const String & string);

    // Egy NULL végu sztringet váró konstruktor
    String(char * str);

    // Egy karaktert és egy elojel nélküli egészet (times) váró konstruktor,
    // amely times darab c karakterrel inicializálja a stringet:
    String(char c, unsigned int times);

    // A destruktor:
    ~String();

    // A string objektum tartalmát a megadott bufferbe másolja, és NULL-lal lezárja
    // (a buffernek a hívó foglal helyet):
    void getStr(char * pBuff);

    // Visszatér a sztring hosszával
    unsigned int getLength(){return elementsNum;}

    // Kiírja a sztringet a megadott kimeneti adatfolyamba (A 'cout' ostream típusú.
    // A .h állományban nem használunk using namespace-t, mert nem tudjuk, hova lesz
    // beépítve, és ott milyen hatása lesz. Ezért kiíjuk az std::-t. Ez a .cpp állományban
    // már nem kell, ot használhatjuk a using namespace std utasítást):
    void print(std::ostream& os);

    // Visszaadja a megadott pozícióban lévo karaktert, egyébként nullát:
    char getChar(unsigned int pos);

    // --- Statikus függvények. Ezek két stringen végeznek muveletet. ---

    // Összefuz két sztringet, és visszatér vele:
    static String concatenate(const String& string1,const String& string2);

    // Összehasonlít két sztringet:
    static bool compare(const String& string1,const String& string2);

    // A második sztringet az elso sztringbe másolja:
    static void copy(String& string1,const String & string2);
    };
    }
    #endif /* STRING_H */

    String.cpp:

    #include <iostream>
    #include <locale>
    #include <string.h>
    #include "String.h"

    using namespace std;
    using namespace HomeMadeString;

    String::String()
    {
    elementsNum=0;
    pData=NULL;
    }

    String::String(const String & string)
    {
    int i;
    this->elementsNum=string.elementsNum;
    this->pData=new char[elementsNum];
    for (i=0;i<elementsNum;i++)
    pData[i]=string.pData[i];
    pData[i]='\0';
    }

    String::String(char *str)
    {
    int i;
    for (i=0;str[i];i++);
    elementsNum=i;
    pData=new char[i];
    for (i=0;str[i];i++)
    pData[i]=str[i];
    pData[i]='\0';
    }

    String::String(char c, unsigned int times)
    {
    int i;
    elementsNum=times;
    if (!times)
    pData=NULL;
    else
    {
    pData=new char [times];
    for (i=0;i<elementsNum;i++)
    pData[i]=c;
    pData[i]='\0';
    }
    }

    String::~String()
    {
    //delete[] pData;
    }

    void String::getStr(char *pBuff)
    {
    int i;
    for (i=0;pData[i];i++)
    pBuff[i]=pData[i];
    pBuff[i]='\0';
    }

    char String::getChar(unsigned int pos)
    {
    if (elementsNum>pos)
    return pData[pos];
    else
    return 0;
    }

    String String::concatenate(const String& string1,const String& string2)
    {
    String str;
    str.elementsNum=string1.elementsNum+string2.elementsNum;
    str.pData=new char[str.elementsNum];
    int i,j;
    for (i=0;string1.pData[i];i++)
    str.pData[i]=string1.pData[i];
    for (j=0;string2.pData[j];j++)
    str.pData[i+j]=string2.pData[j];
    return str;
    }
    bool String::compare(const String& string1,const String& string2)
    {
    if (!(strlen(string1.pData)==strlen(string2.pData)))
    return false;
    for (int i=0;i<string1.elementsNum;i++)
    if (!(string1.pData[i]==string2.pData[i]))
    return false;
    return true;
    }

    void String::copy(String & string1,const String & string2)
    {
    delete[] string1.pData;
    string1.elementsNum=string2.elementsNum;
    if (!string1.elementsNum)
    string1.pData=NULL;
    else
    {
    string1.pData=new char[string1.elementsNum];
    int i;
    for (i=0;string2.pData[i];i++)
    string1.pData[i]=string2.pData[i];
    }
    }

    void String::print(std::ostream & os)
    {
    for (int i=0;i<elementsNum;i++)
    os<<pData[i];
    }

    És a tesztelésre használt VectorSample.cpp:
    #include "Vector.h"
    #include "String.h"

    using namespace std;
    int main()
    {
    Vector v1;

    // Insert tesztelése
    for(int i=1;i<10;i++)
    {
    v1.insert(i,"i");
    }

    // A kiírás (operator<<) és az at() függvény tesztelése
    cout<<v1<<endl;
    //Helyes eredmény:
    // 0 1 2 3 4 5 6 7 8 9

    // Másoló konstruktor
    Vector v2(v1); // Lehetne: Vector v2=v1;
    // op=
    Vector v3;
    v3=v2;

    // Megváltoztatjuk v1-t (erase tesztelése)
    v1.erase(0);
    // v1.erase(9); //Ennek assertelni kell

    cout<<endl<<v1<<endl<<v2<<endl<<v3<<endl;
    // Helyes eredmény:
    // 1 2 3 4 5 6 7 8 9
    // 0 1 2 3 4 5 6 7 8 9
    // 0 1 2 3 4 5 6 7 8 9

    v2.insert(0,"-1");
    cout<<v2<<endl;
    // Helyes eredmény:
    // -1 0 1 2 3 4 5 6 7 8 9

    v2.insert(10,"-1");
    cout<<v2<<endl;
    // Helyes eredmény:
    // -1 0 1 2 3 4 5 6 7 8 -1 9

    v2.insert(12,"-1");
    cout<<v2<<endl;
    // Helyes eredmény:
    // -1 0 1 2 3 4 5 6 7 8 -1 9 -1

    v2.insert(15,"-1");
    cout<<v2<<endl;
    // Helyes eredmény:
    // -1 0 1 2 3 4 5 6 7 8 -1 9 -1 0 0 -1

    v2[15]="-2";
    cout<<v2<<endl;
    // Helyes eredmény:
    // -1 0 1 2 3 4 5 6 7 8 -1 9 -1 0 0 -2

    // v2[16]=3; //Ennek assertelni kell

    return 0;
    }

    Kérem valaki segítsen nekem, már több órája ezzel harcolok, de nem jövök rá, hogy mi a probléma. :S

    Üdvözlettel: Papa

Új hozzászólás Aktív témák