未定义对静态变量的引用

问题描述 投票:0回答:1

我有这个。

#ifndef CUSTOMSTRING_H
#define CUSTOMSTRING_H

using namespace std;

class CustomString{
public:
  CustomString();  //Constructor
  static char *getCandidateString(); //CandidateString Getter
  static void setCandidateString(char *); //CandidateString Setter
  ~CustomString(); //Destructor
private:
  static char *CandidateString; //CandidateString property
};

#endif /* CUSTOMSTRING_H */

还有这个

#include <iostream>
#include "CustomString.h"
#include <string.h>

//Constructor
CustomString::CustomString(){}

//CandidateString Setter
void CustomString::setCandidateString(char *candidateString){
delete[] CandidateString;
int i = strlen(candidateString);
CandidateString = new char[i+1];
strncpy(CandidateString, candidateString, (i+1));
} 

//CandidateString Getter
char *CustomString::getCandidateString(){
return CandidateString;
}

//Destructor
CustomString::~CustomString(){
 delete[] CandidateString;
// cout << "Destroying the CandidateString " << CandidateString << endl;
}

我必须自己用指针来实现String类,而不是使用已经存在的类。我得到以下信息:C:\Users\konst\Desktop\NetbeansProjects\FinalProjectCustomString.cpp:17: undefined reference to `CustomString::CandidateString'显示delete[] CandidateString; 指令为错误。当我把CandidateString实例化到.cpp文件中时,就像这样。CustomString::CandidateString = nullptr; 我得到一个错误,CandidateString是一个私有属性。有什么建议吗?

c++ static
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.