我得到的錯誤error: ‘Name’ was not declared in this scope
缺少什麼我在這裏。使用靜態爲const char * const的,而不是#定義
源文件:
#include<iostream>
#include "scr.h"
using namespace std;
const char* const Test::Name;
void Test::Print()
{
cout<<Name;
}
int main()
{
Test *t = new Test();
t->Print();
delete t;
}
頭文件:
class Test
{
static const char* const Name = "Product Name";
public:
void Print();
};
編輯:
如果我更換char* const
與int
,它的工作原理。爲什麼?
static const int Name = 4; //in header
const int Test::Name; //In source
該代碼的目的是爲有效C++中提到的#define
備用。在那裏的例子中,static const int
被使用。
請參閱編輯的問題 – devnull
@ cod3r - 閱讀我發佈的鏈接。 – adatapost
感謝您的鏈接。這是非常豐富的 – devnull