我試圖做一個向量自自學C++,並再次我就死在什麼似乎並不能修復它。 請原諒你即將看到的可怕的代碼混亂。這也是我在這個網站上的第一篇文章,所以這篇文章的格式可能也會關閉,對不起。嘗試使用模板
有兩個文件:main.cpp中和vect1.h (無vect1.cpp因爲它看起來像只在頭文件掛出模板)
main.cpp中:
#include <iostream>
#include "vect1.H"
using namespace std;
int main(){
vect1<int> inst1(10);
inst1[9]=4;
cout<<inst1[9]<<endl;
//-----
vect1<double> inst2(10);
inst2[5]=5.112;
cout<<inst2[5]<<endl;
//-----
//====PART 2=====//
cout<<"--------"<<endl;
inst2[9]=999;
cout<<inst2[9]<<endl;
//inst2.pop();
inst2.push(2);
cout<<inst2[9]<<endl;
cout<<inst2[10]<<endl;//New block
system("PAUSE");
return 0;}
vect1.h:
#ifndef VECT1_H
#define VECT1_H
#include <iostream> //DEBUG
template <class T>
class vect1{
private:
T *ptr;
T total; //work on this
int units;
int counter;
public:
//vect1();
vect1(T);
vect1();
T &operator[](const int &);
void pop();
void push(T);
};
//---------------------
/*
template <class T>
vect1<T>::vect1(){
ptr = new int [0];
}
*/
template <class T>
vect1<T>::vect1(T number){
ptr = new T [number];
total=0;
units=(int)number;
for(counter=0;counter<number;counter++){
ptr[counter]=0;
}
}
/* now the destruct is giving me errors...
template <class T>
vect1<T>::~vect1(){
total=0;
delete[] ptr;
}*/ //<<this line tosses a C2039 error
template <class T>
T &vect1<T>::operator[](const int & ref){
if(ref>0 && ref<(units)){
return ptr[ref];
}else{
throw "Error! Out of range!"; //<<make catch
}
}
//--------
template <class T>
void vect1<T>::pop(){
units = (units-1);
T *tempPtr;
tempPtr = new T[units];
for(counter=0;counter<units;counter++){
tempPtr[counter]=ptr[counter];
}
delete[] ptr;
ptr = new T[units];
for(counter=0;counter<units;counter++){
ptr[counter]=tempPtr[counter];
}
delete[] tempPtr;
}
//--
template <class T>
void vect1<T>::push(T pushnum){
units++;
const int newsize=(int)units; //<<<<<
T *tempPtr;
tempPtr = new T[units];
for(counter=0;counter<(units-1);counter++){
tempPtr[counter]=ptr[counter];
}
//tempPtr[(int)units]=pushnum;
delete[] ptr;
std::cout<<units<<std::endl;//<<DEBUG
ptr = new T[units];
for(counter=0;counter<(units-1);counter++){
ptr[counter]=tempPtr[counter];
//ptr[9]=101;
}
ptr[newsize]=pushnum; /* <<bleh */
//ptr[newsize]=12321; //DEBUG //<<Even this isn't working...
delete[] tempPtr;
}
//---------------------
#endif
輸出(控制檯):
4
5.112
--------
999
11
999
-6.27744e+066
Press any key to continue . . .
我們的計劃是讓所以當你POP(),它會創建T的新的臨時數組複製一切,但最後一個塊從原始數組到原始數組,刪除原始數組,然後創建一個比以前小一個尺寸的新數組,然後將所有數據發回,刪除臨時數組。推(數字)相同的想法,只是相反。 Push會將自身複製到temp中,push會刪除自身,然後將其自身重新創建爲1更大的尺寸,然後將所有內容從temp推送到temp並刪除temp。然後將推送的號碼發送到新塊中。所以這個程序應該在第二個「999」行之後打印出'2'。但相反,我得到「-6.27744e + 066」。
POP()似乎工作。八九不離十。推(num)雖然有真正的問題。我似乎也突然從我的析構函數中得到一個C2039錯誤。之前沒有這樣做,我還沒有改變任何東西。
我真的很感激,如果有人可以查看這個爛攤子,並給我什麼來解決一些提示。謝謝!
我使用Visual Studio 2010爲我的編譯器。
這是我生成日誌(有一些不錯的錯誤/警告去用它!):
1>------ Rebuild All started: Project: chapter 16-5, Configuration: Debug Win32 ------
1>Build started 2/9/2012 5:34:01 PM.
1>_PrepareForClean:
1> Deleting file "Debug\chapter 16-5.lastbuildstate".
1>InitializeBuildStatus:
1> Creating "Debug\chapter 16-5.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1> main.cpp
1>e:\programming(cpp)\chapter 16-5\chapter 16-5\vect1.h(31): warning C4244: 'initializing' : conversion from 'double' to 'unsigned int', possible loss of data
1> e:\programming(cpp)\chapter 16-5\chapter 16-5\vect1.h(30) : while compiling class template member function 'vect1<T>::vect1(T)'
1> with
1> [
1> T=double
1> ]
1> e:\programming(cpp)\chapter 16-5\chapter 16-5\main.cpp(11) : see reference to class template instantiation 'vect1<T>' being compiled
1> with
1> [
1> T=double
1> ]
1>Manifest:
1> Deleting file "Debug\chapter 16-5.exe.embed.manifest".
1>LinkEmbedManifest:
1> chapter 16-5.vcxproj -> E:\Programming(CPP)\chapter 16-5\Debug\chapter 16-5.exe
1>FinalizeBuildStatus:
1> Deleting file "Debug\chapter 16-5.unsuccessfulbuild".
1> Touching "Debug\chapter 16-5.lastbuildstate".
1>
1>Build succeeded.
1>
1>Time Elapsed 00:00:10.62
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
查看源代碼的'的std :: VECTOR'。實現自定義向量時有很多陷阱(幾乎總是與異常安全有關)。對你更好的可能是學習如何使用* std :: vector。 – 2012-02-09 23:44:28
你的意思是帶有「#包括」的基本載體?我以前使用過它,但從來沒有真正看過它的代碼。不能相信我沒有想到這一點。謝謝! –
Natriacid
2012-02-10 00:09:51