首页 > 解决方案 > 使用指针时未定义的引用

问题描述

每当我使用指针时,我的 type.cpp 都会出现链接器错误。当我不使用指针时,它编译得很好。

类型.h

#pragma once
#include "context.h"
#include <string>
#include <vector>
#include <memory>



template<typename T>
class Clone {
  public:
  T* clone_trait() const {
    T const * t = static_cast<const T*>(this);
    return const_cast<T*>(t);
  }
};


class TypePtr;
struct  IntegralType;
class Type : public Clone<Type> {
public:
  Type(const std::string &name, TypeKind tk,const unsigned int size);



  virtual Type* clone() const =0;
  virtual ~Type();
};

class TypePtr {
  public:
  TypePtr(Type* t);
  TypePtr(const TypePtr&);
  TypePtr(const TypePtr&&);
  TypePtr()=default;
  TypePtr operator=(const TypePtr&);
  Type operator*();
  Type* operator->() const;
  explicit operator bool() const;
  ~TypePtr();
  private:
  Type* t{nullptr};
};

struct IntegralType : Type {
  IntegralType(const std::string &name, Ty ty, const unsigned int size);
  llvm::Type *codegen(FusionCtx &ctx) override;
  Type* clone() const override;
};

struct StructType : Type {
  std::vector<TypedValue> members;
  StructType(const std::string &name, const std::vector<TypedValue> &members);
  llvm::Type *codegen(FusionCtx &ctx) override;
};

类型.cpp

#include "type.h"
#include "error.h"



TypePtr::TypePtr(Type* t) : t(t){};
TypePtr::TypePtr(const TypePtr& o) {
  t = o.t->clone_trait();
}
TypePtr::TypePtr(const TypePtr&& o){
  t=o.t;
}

TypePtr TypePtr::operator=(const TypePtr& o){
  t=o.t->clone_trait();
}

TypePtr::~TypePtr() {
  delete t;
}
TypePtr::operator bool() const {
  return t ? true : false;
}

Type::Type(const std::string &name, TypeKind tk,const unsigned int size)
    : name(name), tk(tk), size(size) {
}





llvm::Type *IntegralType::codegen(FusionCtx &ctx) {
Type* IntegralType::clone() const {
  return new IntegralType(*this);
}

我收到以下错误:

/usr/bin/ld: CMakeFiles/fs.dir/src/codegen.cpp.o: in function `FnProto::codegen(FusionCtx&)':
codegen.cpp:(.text+0x383): undefined reference to `TypePtr::operator->() const'
/usr/bin/ld: CMakeFiles/fs.dir/src/codegen.cpp.o: in function `TypeExpr::codegen(FusionCtx&)':
codegen.cpp:(.text+0xdde): undefined reference to `TypePtr::operator->() const'
/usr/bin/ld: CMakeFiles/fs.dir/src/codegen.cpp.o: in function `VarDeclExpr::codegen(FusionCtx&)':
codegen.cpp:(.text+0x1bcd): undefined reference to `TypePtr::operator->() const'
/usr/bin/ld: CMakeFiles/fs.dir/src/lex.cpp.o: in function `Lexer::stringlit(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
lex.cpp:(.text+0xad2): undefined reference to `TypePtr::operator->() const'
/usr/bin/ld: CMakeFiles/fs.dir/src/parser.cpp.o: in function `Parser::parse_type_expr()':
parser.cpp:(.text+0x3403): undefined reference to `TypePtr::operator->() const'
/usr/bin/ld: CMakeFiles/fs.dir/src/parser.cpp.o:parser.cpp:(.text+0x34c8): more undefined references to `TypePtr::operator->() const' follow
/usr/bin/ld: CMakeFiles/fs.dir/src/type.cpp.o: in function `Type::Type(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Type::TypeKind, unsigned int)':
type.cpp:(.text+0x14b): undefined reference to `vtable for Type'
/usr/bin/ld: CMakeFiles/fs.dir/src/type.cpp.o: in function `Type::getI8()':
type.cpp:(.text+0x4b9): undefined reference to `IntegralType::IntegralType(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, IntegralType::Ty, unsigned int)'
/usr/bin/ld: CMakeFiles/fs.dir/src/type.cpp.o: in function `Type::getI16()':
type.cpp:(.text+0x5dc): undefined reference to `IntegralType::IntegralType(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, IntegralType::Ty, unsigned int)'
/usr/bin/ld: CMakeFiles/fs.dir/src/type.cpp.o: in function `Type::getI32()':
type.cpp:(.text+0x6fc): undefined reference to `IntegralType::IntegralType(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, IntegralType::Ty, unsigned int)'
/usr/bin/ld: CMakeFiles/fs.dir/src/type.cpp.o: in function `Type::getI64()':
type.cpp:(.text+0x81c): undefined reference to `IntegralType::IntegralType(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, IntegralType::Ty, unsigned int)'
/usr/bin/ld: CMakeFiles/fs.dir/src/type.cpp.o: in function `Type::getISize()':
type.cpp:(.text+0x93b): undefined reference to `IntegralType::IntegralType(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, IntegralType::Ty, unsigned int)'
/usr/bin/ld: CMakeFiles/fs.dir/src/type.cpp.o:type.cpp:(.text+0xa5c): more undefined references to `IntegralType::IntegralType(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, IntegralType::Ty, unsigned int)' follow
/usr/bin/ld: CMakeFiles/fs.dir/src/type.cpp.o: in function `IntegralType::~IntegralType()':
type.cpp:(.text._ZN12IntegralTypeD2Ev[_ZN12IntegralTypeD2Ev]+0x14): undefined reference to `Type::~Type()'
/usr/bin/ld: CMakeFiles/fs.dir/src/type.cpp.o: in function `StructType::~StructType()':
type.cpp:(.text._ZN10StructTypeD2Ev[_ZN10StructTypeD2Ev]+0x3d): undefined reference to `Type::~Type()'
/usr/bin/ld: CMakeFiles/fs.dir/src/type.cpp.o: in function `Type::Type(Type const&)':
type.cpp:(.text._ZN4TypeC2ERKS_[_ZN4TypeC2ERKS_]+0xb): undefined reference to `vtable for Type'
/usr/bin/ld: CMakeFiles/fs.dir/src/type.cpp.o:(.data.rel.ro+0x40): undefined reference to `typeinfo for Type'
/usr/bin/ld: CMakeFiles/fs.dir/src/type.cpp.o:(.data.rel.ro+0x88): undefined reference to `typeinfo for Type'
clang-9: error: linker command failed with exit code 1 (use -v to see invocation)

如果我在 TypePtr 中将 clone_trait() 更改为 clone() ,我会得到同样的错误,当我使用 typePtr 的 unique_ptr insted 时,我会得到同样的错误。我应该怎么办?

标签: c++c++11pointersclang++

解决方案


推荐阅读