c++ - 内联函数和 __thiscall __cdecl Bjarne String 示例

标签 c++ string visual-c++ calling-convention

我在 Bjarne“C++...”中实现了 String 类。我想要内联 read() 和其他访问器函数,所以我将它们标记为内联。没关系,但是定义对主文件中对类 String 的引用进行读取的哈希函数会给出错误 LNK2019 未解析的外部符号 char __thiscall String::read(int)const

为什么这个没有解决?

String.h 中的 String 类:

#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;
};

字符串.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;
}

主文件:

// 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;
}

最佳答案

要使函数内联,它应该在标题中定义。 如果您真的想要内联函数,请将定义移动到标题或将它们移动到类的主体并删除内联。 要让 mo linkage arrors 在 read

之前删除内联说明符

关于c++ - 内联函数和 __thiscall __cdecl Bjarne String 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14006315/

相关文章:

c++ - 无法使用lambda参数编译boost::spirit::x3解析器

c++ - 在 DirectX 11.2 C++ 中围绕所有 3 个(X、Y、Z) Axis 旋转模型

c# - 将整个对象数组转换为字符串

swift - Swift 中的 DNA 到 RNA 转录

c++ - 视觉 C++ : how to pass pointer to array as parameter?

c++ - 针对自定义构建的 tcl85.lib 的链接器错误,适用于 ActiveState 分发的 tcl85.lib

c++ - 检查传递的双参数是否为 "close enough"以被视为整数

c++ - 找不到G++库

javascript - js中克隆的substring()函数出错

c++ - 在对可执行文件大小没有严格限制的情况下,为什么在 Visual C++ 9 中更喜欢/Ob1 而不是/Ob2?