·c++程序代碼的基本形式:
c++文用include使用頭文件。
用<>包括標準庫。
用””包括自定義頭文件。
·Guard(防衛(wèi)式聲明):
#ifndef _COMPLEX_
#define _COMPLEX_
.
.
#endif
只有未被定義的COMPLEX(自己取的名稱)才會被定義
當?shù)诙伪欢x時會跳過。
·類:
1帶指針
2不帶指針。多半不用析構(gòu)函數(shù)。
·singleton:單鍵,單例。
·同一個class各個對象互為友元。
·左值L-value中的L指的是Location,表示可尋址。
右值R-value中的R指的是Read,表示可讀。
寫作業(yè)的一些問題:
1.error: expected constructor, destructor, or type conversion before '(' token|
srand((unsigned)time(NULL));應(yīng)該寫在main內(nèi)或函數(shù)中。
2.Date *date=CreatePoints(d);
date指向一個大小為10的數(shù)組,因為數(shù)組名本身就是一個常量地址,所以后面date[i].print();可以不用再date前加 *。
</br>
<br />
前幾天寫課程設(shè)計的一些問題和報錯:
·在重載運算函數(shù)中無法直接初始化引用的類,只能另寫get函數(shù)賦值。
·運用fstream剛開始分不清程序和txt之間的讀入讀出,出了很多錯誤。
·讀取txt文檔信息時,本想用seekg()函數(shù),但是無法按行跳過,只能用getline逐行讀取。
·修改用了一個比較笨的方法,讀取文件,存入vector<string>,關(guān)閉文件,修改vector,用ios::out清空文件,存入vector。
·warning C4786: (只在vc6.0中報錯)
解決方法有兩種,一種是直接定義別名:
#ifdef _DEBUG
#define VeryLongClassNameA A
#define VeryLongClassNameB B
#endif
另一種是屏蔽4786warning:
#pragma warning(disable : 4786)
·使用vc6.0時,文件不能正確讀取,而在vs2015和codeblocks中正常,懷疑是vc6.0中ofstream和其他編譯器不同,后發(fā)現(xiàn)ifstream后標記符為ios::app,改正后正常。
·change函數(shù)不能輸出到文件,調(diào)錯了很長時間才發(fā)現(xiàn)是最后的save函數(shù)覆蓋掉了。
權(quán)衡了一下,把save函數(shù)刪掉了。
·Find函數(shù)中,如果把if條件換成string1==string2,就會報錯,關(guān)于堆棧方面的。后來想到了比較函數(shù)string1.compare(string2),問題解決。但不知道為什么會出現(xiàn)這種錯誤。
·讀取文件夾下文件列表。照著下面的學習了一下,但我刪掉了判斷文件夾的部分,加入了創(chuàng)建文件夾部分。
void getAllFiles(string path, vector<string>& files) {
//文件句柄
long hFile = 0;
//文件信息
struct _finddata_t fileinfo; //很少用的文件信息讀取結(jié)構(gòu)
string p; //string類很有意思的一個賦值函數(shù):assign(),有很多重載版本
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo)) != -1) {
do {
if ((fileinfo.attrib & _A_SUBDIR)) { //比較文件類型是否是文件夾
if (strcmp(fileinfo.name,".") != 0 && strcmp(fileinfo.name,"..") != 0) {
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
getFilesall(p.assign(path).append("\\").append(fileinfo.name), files);
}
} else {
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0); //尋找下一個,成功返回0,否則-1
_findclose(hFile);
}
}
部分報錯:
1.vector<string> read(peccancy &l);報錯
2.error: ambiguating new declaration of 'std::vector<std::basic_string<char> > read(peccancy&)'|
二義性,read前加&引用。
3.for(vector<string>::iterator iter=str.begin(); iter!=str.end(); iter++)原來缺少”!”報錯。
4.rename(oldadd.c_str(),newadd.c_str());原型rename(char ,char ),不能直接傳string
應(yīng)該使用.c_str()函數(shù)轉(zhuǎn)化為const char。
5.cin.getline() 原型為cin.getline(const char ,streamsize n ) 無法傳入string形參,
換用string::getline()。
error: no matching function for call to 'std::basic_ifstream<char>::getline(std::string&, int)'|
6.個別if判斷字符用了””,報錯。
error: ISO C++ forbids comparison between pointer and integer [-fpermissive]|
7頭文件在include時重復(fù)定義(multiple definition of)
為了方便循環(huán)使用吧int i,j;放在頭文件里,出現(xiàn)問題。
解決:刪除,在函數(shù)中重新定義。