최근에 std :: string의 하위 클래스 spstring을 정의하려고합니다. 그것은 spstr.h 선언 : MAIN.CPP에서 테스트std :: string을 상속하는 방법은 무엇입니까?
그러나#include <cctype>
#include <string>
#include <algorithm>
#include <sstream>
#include <stdint.h>
#include <xstring>
class spstring : public std::string {
public:
spstring(std::string s):std::string(s){} //Declare the constructor
int stoi(); //Declare stoi
spstring Spstring(std::string s); ////Declare mandatory conversion function
};
spstring spstring::Spstring(std::string s)
{
spstring spstr(s);
return(spstr);
}
:
오류 C2228 :
spstring byteaddstr(std::string(argv[4])); //convertchar* to spstring int byteadd; byteadd=byteaddstr.stoi(); //call byteaddstr.stoi
는 실패가에 대한 적합하여야한다 "의 왼쪽. stoi "클래스/구조체/공용체가 있어야합니다.
실제로 이상한 소리가납니다. 사실, byteaddstr은 실제로 spstring 왜 멤버 함수를 호출 할 수 없습니까?
일반적으로 STL 컨테이너에서 파생되기를 원하지 않습니다. –
일반적으로'std :: string'을 상속하는 것은 좋지 않습니다. 특히 공개적으로 (관련 SO 게시물 참조) (http://stackoverflow.com/questions/6006860/why-should-one-not -derive-from-c-std-string-class). private 상속 *은 클래스와'std :: string' 사이의 밀접한 결합을 강요하지만 충분히 안전하다고 생각할 수도 있습니다. [상속의 남용]에 대한 흥미로운 토론을 참조하십시오 (http://www.gotw.ca/publications/mill06.htm).오류에 관해서는, 문제를 재현하는 자그마한 예제를 게시하십시오. – juanchopanza
std :: string (또는 map, vector 또는 다른 컨테이너)에서 파생되지 않습니다. 이 문제가 발생한 모든 경우에 사용할 수있는 몇 가지 더 나은 솔루션이있었습니다. stl 컨테이너에서 파생해야하는 경우 컨테이너를 클래스 멤버로 사용하거나 개인 상속을 사용하는 대신 캡슐화를 고려하십시오. – Peter