0

好吧,因为我遇到了一些未定义的类问题,因为一个头文件正在添加另一个正在添加自己的头文件,它可能正在编译一些当时不存在的类,所以我做了这个并放在开头,编译正常,但是在编译的最后一秒它抛出了一个错误

 Error  230 error LNK2020: unresolved token (0600003C) Servicos::.ctor  Servicos.obj

-

public ref class Servicos: public System::Windows::Forms::Form {
    public:
    Servicos(Usuario*,unordered_map<int, std::string>*);
}

由于当时我并不关心其他函数,所以我只放置了构造函数,因为它所做的只是实例化类以使对话框显示。(虽然我也尝试过添加所有功能,但发生了同样的错误)

这是原文:

namespace MyProject {
public ref class Servicos: public System::Windows::Forms::Form
    {

    //... Some Variables declared

    public: 
        Servicos(Usuario* user, 
            unordered_map<int, std::string>* fab_contas_)
        {
            this->fab_contas_email = fab_contas_;
            this->usuario = user;
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

//... And Other Functions
};

}

你们中的任何人都可以指出我做错了什么吗?非常感谢!谢谢!

4

1 回答 1

1

这对我来说没有多大意义:链接器告诉你类的构造函数Servicos没有定义。编译进入链接阶段这一事实意味着(显式或隐式)声明了ctor。

公共参考类 Servicos:公共 System::Windows::Forms::Form { 公共:ServicosFacebook(Usuario*,unordered_map*); }

可能是这段代码导致了问题,尽管它有语法错误,所以这一定不是您实际使用的代码(否则您将无法进入链接阶段)——该函数ServicosFacebook不是 ctor 并且没有返回类型。

我假设你有这个:

public ref class Servicos: public System::Windows::Forms::Form { 
  public: 
  Servicos(Usuario*,unordered_map<int, std::string>*); 
} 

这会通知编译器有一个 type 的构造函数(带有两个参数)Servicos,但它是在其他地方定义的——这就是导致链接器错误的问题,因为您实际上并没有提供定义。

但是,您定义构造函数的类的定义位于MyProject命名空间中,因此是完全不同的类型。

将上面的类声明放入命名空间是不够的,MyProject因为这会违反单一定义规则:你只能有一个类的定义,但你会有两个(即使相同)。

要修复,您需要解决头文件中的循环依赖关系。由于您没有提供足够的信息,因此我无法为您提供太多帮助。如果您只需要类存在的概念,则可以在标头中使用前向声明来打破循环依赖:

class Servicos;

这将允许您声明指向该类的指针(作为成员变量或函数参数)。您必须在 .cpp 文件中包含正确的 Servicos 标头(而不是在其标头中)

更新:

尝试:

// Servicos.h
// include headers that contain the definition of Windows Forms, Usuario,
// unordered_map, string, etc.
using namespace std;
namespace MyProject {  
  public ref class Servicos: public System::Windows::Forms::Form  
  {  

    //... Some Variables declared  

  public:   
    Servicos(Usuario* user,   
        unordered_map<int, std::string>* fab_contas_);

    //... And Other Functions  
  };  

}  

// Servicos.cpp
#include "Servicos.h"
using namespace MyProject;

Servicos::Servicos(Usuario* user,   
        unordered_map<int, std::string>* fab_contas_)  
{  
  this->fab_contas_email = fab_contas_;  
  this->usuario = user;  
  InitializeComponent();  
  //  
  //TODO: Add the constructor code here  
  //  
}  

//... And Other Functions  

// ServicosUser.h
#include "Servicos.h"

// ... declarations that use Servicos

// ServicosUser.cpp
#include "ServicosUser.h"

// ... definitions of things declared in ServicosUser.h
于 2012-04-16T00:26:41.813 回答