C++ 在结构中定义构造函数

C++ 在结构中定义构造函数,c++,struct,constructor,C++,Struct,Constructor,尝试查看结构和构造函数在头文件、实现文件和主文件中的工作方式。使用构造函数和默认构造函数。我在mains.cpp中得到“未定义对'numbers::numbers()的引用”的编译错误 在测试h中,我有: #ifndef H_TEST #define H_TEST struct numbers{ int a; int b; numbers(); numbers(int x, int y); }; #endif 在Numbers.cpp中,我有: #include "test

尝试查看结构和构造函数在头文件、实现文件和主文件中的工作方式。使用构造函数和默认构造函数。我在mains.cpp中得到“未定义对'numbers::numbers()的引用”的编译错误

在测试h中,我有:

#ifndef H_TEST
#define H_TEST

struct numbers{

   int a;
   int b;
numbers();

numbers(int x, int y);

};
#endif
在Numbers.cpp中,我有:

#include "test.h"

 numbers::numbers()
  {
     a=0;
     b=0;
  }
  numbers::numbers(int x, int y)
  {
      a=x;
      b=y;
  }
 #include<iostream>
 #include "test.h"
 using namespace std;

 numbers num;//compilation error occurs here
 int main()

{





 return 0;
 }
在mains.cpp中,我有:

#include "test.h"

 numbers::numbers()
  {
     a=0;
     b=0;
  }
  numbers::numbers(int x, int y)
  {
      a=x;
      b=y;
  }
 #include<iostream>
 #include "test.h"
 using namespace std;

 numbers num;//compilation error occurs here
 int main()

{





 return 0;
 }
#包括
#包括“test.h”
使用名称空间std;
numbers num;//此处发生编译错误
int main()
{
返回0;
}

看起来您是在头文件中声明内联构造函数,方法是为构造函数放入函数体(尽管是空函数体)

我希望在包含头的文件中,当编译器看到内联定义时,它将使用这些定义,因此不会生成与.cpp文件中的定义链接的符号,因此不会调用.cpp文件中的定义


尝试删除头文件中的空函数体。

通过为构造函数放入函数体(尽管是空函数体),看起来您正在头文件中声明内联构造函数

我希望在包含头的文件中,当编译器看到内联定义时,它将使用这些定义,因此不会生成与.cpp文件中的定义链接的符号,因此不会调用.cpp文件中的定义


尝试删除标题中的空函数体。

问题在于,默认情况下,您正在构造
num
,而没有重新分配它

numbers num; // Constructs a numbers object with a = 0, b = 0 and stores it in num.
int main()
{
    numbers(3,5); // Constructs a numbers object with a = 3, b = 5.
                  // The object is discarded after the constructor call finishes.

    cout<<num.a; // Prints a from the global variable num.

    return 0;
}
numbers num;//构造一个a=0、b=0的numbers对象,并将其存储在num中。
int main()
{
numbers(3,5);//构造一个a=3,b=5的numbers对象。
//构造函数调用完成后,将丢弃该对象。

cout问题是您默认构造
num
,而不是重新分配它

numbers num; // Constructs a numbers object with a = 0, b = 0 and stores it in num.
int main()
{
    numbers(3,5); // Constructs a numbers object with a = 3, b = 5.
                  // The object is discarded after the constructor call finishes.

    cout<<num.a; // Prints a from the global variable num.

    return 0;
}
numbers num;//构造一个a=0、b=0的numbers对象,并将其存储在num中。
int main()
{
numbers(3,5);//构造一个a=3,b=5的numbers对象。
//构造函数调用完成后,将丢弃该对象。


最可能的原因是你没有正确链接,你只链接了
mains.cpp
。它的可能副本在MSVS 2013中运行良好。你使用了哪一个?我使用的是代码块你在test.h中将
numbers::numbers(){}
定义为一个空函数。它从不调用
numbers::numbers())
在numbers.cpp中最可能的原因是您没有正确链接,您只链接了
mains.cpp
。它的可能副本在MSVS 2013中运行良好。您使用了哪一个?我使用的是CodeBlocks您正在定义
numbers::numbers(){}
作为test.h中的空函数。它从不调用numbers.cpp中的
numbers::numbers()
,除非我添加{},否则它不会编译;在头文件中。@Jane那是另一个问题。你应该发布你得到的错误,因为如果你想在别处定义函数体,头中的空函数体是错误的。除非我添加{},否则它不会编译;在头文件中。@Jane那是另一个问题。你应该发布你得到的错误,因为如果你想在别处定义函数体,头中的空函数体是错误的。是的,我得到了对numbers::numbers()和numbers(int,int)的未定义引用的编译错误在mains.cpp中,您提到您正在使用Code::Blocks。您是否将Numbers.cpp添加到项目中?(它可能会出现在源代码下。)是的,它是我项目中的一个源文件。我最终能够将其编译。我不知道为什么以前不会!是的,我收到了对Numbers::Numbers()和Numbers的未定义引用的编译错误(int,int)来自mains.cpp您提到您正在使用Code::Blocks。您是否将Numbers.cpp添加到项目中?(它可能会出现在源代码下。)是的,它是我项目中的一个源文件。我最终能够将其编译。我不知道为什么以前不会!