Hirdetés
- Mindenkinél több és erősebb AI gyorsítót ígér Elon Musk
- M.2 csatlakozók terén (is) jónak ígérkezik az MSI közelgő AMD-s alaplapja
- Kivégezheti a kisebb VGA-gyártókat az NVIDIA döntése
- Szinte a semmiből robbanna be az 1,4 nm-es eljárásával a Rapidus
- Félszáz terabájtos HDD-k előtt nyitotta ki az ajtót a Seagate
- Gyors, éles, olcsó: Acer Predator XB273K V5 teszt
- Apple asztali gépek
- AMD Ryzen 9 / 7 / 5 9***(X) "Zen 5" (AM5)
- Szinte a semmiből robbanna be az 1,4 nm-es eljárásával a Rapidus
- AMD K6-III, és minden ami RETRO - Oldschool tuning
- Nem indul és mi a baja a gépemnek topik
- Milyen házat vegyek?
- Nvidia GPU-k jövője - amit tudni vélünk
- Azonnali alaplapos kérdések órája
- eGPU tapasztalatok
Új hozzászólás Aktív témák
-
papa019
senior tag
Sziasztok.
Van egy hatalmas problémám. :SStringekbő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
● ha kódot szúrsz be, használd a PROGRAMKÓD formázási funkciót!
- Eladó Samsung 24" Full HD LED monitor (S24C450B)
- 2013 Late 27 iMac - 1TB HDD i5 core4 24GB RAM 2GB GTX
- Bomba ár! Toshiba Portege R930 - i5-3GEN I 4GB I 320GB I DVDRW I 13,3" HD I HDMI I Cam I W10 I Gari!
- Bomba ár! Toshiba Portege X30-E - i5-8250U I 8GB I 256SSD I 14" FHD I Cam I W11 I Garancia!
- Bomba ár! Toshiba Satellite Pro A40-D - i5-7200U I 8GB I 256SSD I 14" HD I Cam I W11 I Garancia!
- BESZÁMÍTÁS! Logitech G920 Driving Force Racing kormányszett
- Telefon felvásárlás!! iPhone 13 Mini/iPhone 13/iPhone 13 Pro/iPhone 13 Pro Max
- Samsung Galaxy S22 5G 128GB, Kártyafüggetlen, 1 Év Garanciával
- LG UltraFine 4K és Smart Monitorok: BLACK NOVEMBER -30%
- Apple iPhone 14 Pro Max / Kártyafüggetlen / 256GB / 12Hó Garancia / 87% akku
Állásajánlatok
Cég: BroadBit Hungary Kft.
Város: Budakeszi
Cég: PCMENTOR SZERVIZ KFT.
Város: Budapest


