Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 内联函数和_thiscall _cdecl Bjarne字符串示例_C++_String_Visual C++_Calling Convention - Fatal编程技术网

C++ 内联函数和_thiscall _cdecl Bjarne字符串示例

C++ 内联函数和_thiscall _cdecl Bjarne字符串示例,c++,string,visual-c++,calling-convention,C++,String,Visual C++,Calling Convention,我在比亚恩的“C++…”中实现了字符串类。我想让read()和其他访问器函数内联,所以我将它们标记为内联。可以,但定义对主文件中类字符串的引用执行读取的哈希函数会导致错误LNK2019未解析的外部符号char\uuu thiscall String::read(int)const 为什么这个问题没有解决 String.h中的类字符串: #include <string.h> class String{ struct Srep{ // representation

我在比亚恩的“C++…”中实现了字符串类。我想让read()和其他访问器函数内联,所以我将它们标记为内联。可以,但定义对主文件中类字符串的引用执行读取的哈希函数会导致错误LNK2019未解析的外部符号char\uuu thiscall String::read(int)const

为什么这个问题没有解决

String.h中的类字符串:

#include <string.h>

class String{
    struct Srep{  // representation
        char* s; // pointer to elements
        int sz;  // number of characters
        int n;   // reference counter
        Srep(int nsz, const char* p){
            n=1;
            sz=nsz;
            s=new char[sz+1]; // add space for terminator
            strcpy(s,p);
        }
        ~Srep(){delete[]s;}
        Srep* get_own_copy(){ // clone if necessary
            if(n==1)return this;
            n--;
            return new Srep(sz,s);
        }
        void assign(int nsz, const char* p){
            if(sz!=nsz){
                delete[]s;
                sz=nsz;
                s=new char[sz+1];
            }
            strcpy(s,p);
        }
    private: //prevent copying
        Srep(const Srep&);
        Srep& operator=(const Srep&);
    };
    Srep* rep;
public:
    class Cref{ // reference to char
        friend class String;
        String& s;
        int i;
        Cref(String& ss, int ii):s(ss), i(ii){}
    public:
        inline operator char(){                 // yield value
            return s.read(i);
        }
        inline void operator=(char c){              // change value
            s.write(i,c);
        }
    };
    class Range{};

    String();                       // x=""
    String(const char*);            // x="abnm'
    String(const String&);          // x=other_string
    String& operator=(const char*);
    String& operator=(const String&);
    ~String();
    // access operators
    void check(int i)const;
    inline char read(int i)const;
    void write(int i, char c);
    Cref operator[](int i);
    char operator[](int i)const;
    int size()const;
};
#包括
类字符串{
结构Srep{//表示
char*s;//指向元素的指针
int sz;//字符数
int n;//引用计数器
Srep(内部nsz,常量字符*p){
n=1;
sz=nsz;
s=新字符[sz+1];//为终止符添加空间
strcpy(s,p);
}
~Srep(){delete[]s;}
Srep*获取自己的副本(){//必要时克隆
如果(n==1),则返回该值;
n--;
返回新的Srep(深圳、深圳);
}
无效分配(整数nsz,常量字符*p){
如果(sz!=nsz){
删除[]s;
sz=nsz;
s=新字符[sz+1];
}
strcpy(s,p);
}
私人文件:防止复制
Srep(const-Srep&);
Srep&运算符=(常量Srep&);
};
Srep*代表;
公众:
类Cref{//对char的引用
好友类字符串;
字符串&s;
int i;
Cref(String&ss,intii):s(ss),i(ii){
公众:
内联运算符char(){//yield value
返回s.read(i);
}
内联void运算符=(字符c){//更改值
s、 写(i,c);
}
};
类范围{};
String();//x=“”
字符串(const char*);//x=“abnm”
字符串(常量字符串&);//x=其他字符串
字符串和运算符=(常量字符*);
字符串和运算符=(常量字符串&);
~String();
//接入运营商
无效检查(int i)常数;
内联字符读取(int i)常量;
无效写入(int i,char c);
Cref运算符[](int i);
字符运算符[](int i)常量;
int size()常量;
};
String.cpp:

#include "stdafx.h"
#include "String.h"

String::String(){       // the empty string is the default value
    rep=new Srep(0,"");
}
String::String(const String& x){    // copy constructor
    x.rep->n++;
    rep=x.rep; // share representation
}
String::~String(){
    if(--rep->n==0)delete rep;
}
String& String::operator=(const String& x){ // copy assignment
    x.rep->n++; // protects against "st=st"
    if(--rep->n==0)delete rep;
    rep=x.rep;
    return *this;
}
// pseudo-copy operations taking const char* args are provided to allow
//string literals
String::String(const char* s){
    rep=new Srep(strlen(s),s);
}
String& String::operator=(const char* s){
    if(rep->n==1)   // recycle Srep
        rep->assign(strlen(s),s);
    else{           //use new Srep
        rep->n--;
        rep=new Srep(strlen(s),s);
    }
    return *this;
}
// access operators
inline void String::check(int i)const{
    if(i<0||rep->sz<=i)throw Range();
}
inline char String::read(int i)const{ // unchecked access to s
    return rep->s[i];
}
inline void String::write(int i, char c){
    rep=rep->get_own_copy();
    rep->s[i]=c;
}
inline String::Cref String::operator[](int i){
    check(i);
    return Cref(*this,i);
}
inline char String::operator[](int i)const{// checked access to s
    check(i);
    return rep->s[i];
}
inline int String::size()const{
    return rep->sz;
}
#包括“stdafx.h”
#包括“String.h”
String::String(){//空字符串是默认值
rep=新Srep(0,“”);
}
String::String(conststring&x){//复制构造函数
x、 rep->n++;
rep=x.rep;//共享表示
}
字符串::~String(){
如果(--rep->n==0)删除rep;
}
字符串和字符串::运算符=(常量字符串和x){//复制赋值
x、 rep->n++;//防止“st=st”
如果(--rep->n==0)删除rep;
rep=x.rep;
归还*这个;
}
//提供了采用const char*args的伪复制操作,以允许
//字符串文字
字符串::字符串(常量字符*s){
rep=新的Srep(strlen,s);
}
字符串和字符串::运算符=(常量字符*s){
如果(rep->n==1)//回收Srep
代表->分配(strlen,s);
否则{//使用新的Srep
代表->n--;
rep=新的Srep(strlen,s);
}
归还*这个;
}
//接入运营商
内联无效字符串::检查(int i)常量{
if(iszs[i];
}
内联void字符串::write(int i,char c){
rep=rep->get_own_copy();
rep->s[i]=c;
}
内联字符串::Cref字符串::运算符[](int i){
支票(i);
返回Cref(*本,i);
}
内联字符字符串::运算符[](int i)常量{//已检查对s的访问
支票(i);
返回rep->s[i];
}
内联int字符串::size()常量{
返回rep->sz;
}
主文件:

// Bjarne_exercise_string.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "String.h"

int hash(const String& s){
    //int h=s.read(0);
    //const int max=s.size();
    //for(int i=1;i<max;i++){
    //  h^=s.read(i)>>1; // unchecked access to s (1st take s[i], then rightshift, and then XOR h and this)
    //}
    //return h;
    return 4;
}

int _tmain(int argc, _TCHAR* argv[])
{
    String s1;
    String s2;
    String s3="s3";
    String s4("s4");
    String s5(s3);
    s5=s4;
    s3=s4;
    //int i=hash(s3);
    //i=hash(s1);
    return 0;
}
//Bjarne\u exercise\u string.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括“String.h”
int散列(常量字符串&s){
//int h=s.read(0);
//常量int max=s.size();
//for(int i=1;i>1;//未选中对s的访问(先取s[i],然后右移,然后是XOR h和this)
//}
//返回h;
返回4;
}
int _tmain(int argc,_TCHAR*argv[]
{
字符串s1;
字符串s2;
字符串s3=“s3”;
字符串s4(“s4”);
串s5(s3);
s5=s4;
s3=s4;
//int i=散列(s3);
//i=散列(s1);
返回0;
}

您没有
字符串::read()
函数。实现它,您将获得黄金[或至少更近一些!]

函数的主体不能内联,除非编译器在调用站点可以使用它们。将所有内联函数移到头文件中


至于您的链接器错误,您是否记得链接到
String.obj

要使函数内联,它应该在标题中定义。 若确实需要内联函数,请将定义移动到类的头或将其移动到类的主体并删除内联函数。
有MO链接数组删除代码> >代码> Re><代码> < /P>为什么你决定只在C++中嵌入一些函数?<代码>内联< /代码>,不意味着“请优化这个”",如果这是您的想法。编译器可以自由内联或不内联它喜欢的任何函数。
inline
关键字更改函数的链接方式,以便多个翻译单元可以各自具有同一函数的定义。这不是主题,问题是为什么内联函数无法解决它可能发生在o您是否知道,由于您不理解的原因而导致的链接器错误与您在代码中插入不理解的关键字的决定之间可能存在联系?除非这是一个技巧性问题,并且您已经知道答案,否则我建议您对人们告诉您的内容更加开放一些。可能评论或回答您问题的人可能知道一些您不知道的事情,即
inline
函数也将是其编译单元的本地函数。这与在.cpp文件中定义它不太合适。它在头文件.Ah中工作得更好,但在.h文件中工作得更好,因此当编译器编译使用inline函数的文件时,它会这样做esn没有看到它。很抱歉,我没有在“错误的文件”中查找。它应该在头文件中。不必抱歉。我不会问我是否将此放在了正确的pla中