我想用名稱爲「a」的const int變量創建類Test。之後,我需要創建構造函數,其中變量「a」的值爲10。我創建一流的試驗和test.h我在課堂上Test.h創建const int a;
和TEST.CPP我有類似的東西:C++並初始化常量變量
#include "stdafx.h"
#include "Test.h"
Test::Test(void)
{
a = 10;
b = 20;
size = 20;
tekst[size];
}
Test::~Test(void)
{
}
,這是test.h:
#pragma once
class Test
{
const int a;
public:
Test(void);
~Test(void);
int b;
char *tekst;
int size;
static double d;
int y;
};
但我得到錯誤:
Error 1 error C2758: 'Test::a' : must be initialized in constructor base/member initializer list c:\users\bożydar\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\test.cpp 6
Error 2 error C2166: l-value specifies const object c:\users\bożydar\documents\visual studio 2012\projects\consoleapplication1\consoleapplication1\test.cpp 7
我敢肯定,搜索*構造基礎/成員初始化列表*在谷歌將拉動一些好成績。 – chris