不是我所有的构造函数都被导入了吗?
Not all of my constructors are being imported?
我正在制作一个堆 class 可以用 heap.h
导入,我的构造函数包括 bool 类型不起作用,但所有其他构造函数和函数导入都可以。
这是 heap.h
中的内容:
#ifndef __HEAP_INCLUDED__
#define __HEAP_INCLUDED__
#include <iostream>
#include <vector>
using namespace std;
class heap{
int capacity;
bool isMinHeap; //1 is min heap -- ascending order
vector<int> * content;
public:
heap();
heap(bool t);
heap(vector<int> * input);
heap(vector<int> * input, bool t);
void print();
void prettyPrint();
int parent(int i);
int leftChild(int i);
int rightChild(int i);
int size();
int getMax();
void insert(int data);
void heapifyDown(int index);
void heapifyUp(int index);
int invalidChild(int index);
int deleteMax();
int deleteMin();
bool minDir();
int at(int index);
};
vector<int> * heapSort(vector<int> * input);
void swap(vector<int> * vec, int a, int b);
#endif
这里是heap.cpp
中定义的构造函数。请注意,当我向该文件添加一个 main 以测试内容时,所有构造函数都可以正常工作:
class heap{
vector<int> * content;
int capacity = 256;
bool isMinHeap; //1 is min heap -- ascending order
public:
heap(){
content = new vector<int>;
isMinHeap = 0;
}
heap(bool t){
content = new vector<int>;
isMinHeap = t;
}
heap(vector<int> * input){
content = input;
isMinHeap = true;
for(int i = content->size()/2; i >= 0; i--){
heapifyDown(i);
}
}
heap(vector<int> * input, bool t){
content = input;
isMinHeap = t;
for(int i = content->size()/2; i >= 0; i--){
heapifyDown(i);
}
}
//other functions below
}
带有 bool 的构造函数在 main.cpp
中不起作用,#include "heap.h"
在顶部。这些文件都在同一目录中,我正在使用此命令进行编译:g++ heap.cpp main.cpp -o main
。为什么我的两个构造函数不起作用?
我看到的错误是
/usr/bin/ld: /tmp/ccwomODk.o: in function `main':
main.cpp:(.text+0x4e2): undefined reference to `heap::heap(bool)'
collect2: error: ld returned 1 exit status
-Wall
没有详细说明这个问题。我很确定问题出在我的链接上,因为当我在那里使用构造函数时,它们在 heap.cpp
内部工作。
您对 .cpp
文件中的 class 所做的操作是错误的。您不能定义 class 两次。程序中必须只有一个class heap { /*...*/ };
(但它可能包含在多个.cpp
文件中)。否则违反 one-definition-rule (ODR) 并且程序具有未定义的行为。
因此,请从 heap.cpp
中删除您显示的所有内容。
要在heap.cpp
文件中定义heap
的构造函数,需要使用这样的语法:
#include "heap.h"
heap::heap() {
/*...*/
}
heap::heap(bool t) {
/*...*/
}
//...
等等。其他成员函数必须以类似的方式定义,例如:
void heap::print() {
/*...*/
}
此外,如果你想有一个默认的成员初始值设定项,如
int capacity = 256;
将其添加到 .h
文件的声明中。
我还想补充一点,让 pointer-to-std::vector
作为成员几乎肯定也是一种错误的做法,但 out-of-scope 是个问题。
When you declare a program element such as a class, function, or
variable, its name can only be "seen" and used in certain parts of
your program. The context in which a name is visible is called its
scope. For example, if you declare a variable x within a function, x
is only visible within that function body.
看来你严重违反了 ODR 规则。您的 class 成员包括构造函数没有在源文件中声明主体 (heap.cpp)。
使用 '::' 使 class 成员拥有正文:
//heap.cpp
"heap.h"
heap::heap()
{
}
heap:heap(vector<int> * input, bool t)
{
}
int heap::parent(int i)
{
return i;
}
// this is how you create a body for function that are class members
// the same should be done for all other functions
我正在制作一个堆 class 可以用 heap.h
导入,我的构造函数包括 bool 类型不起作用,但所有其他构造函数和函数导入都可以。
这是 heap.h
中的内容:
#ifndef __HEAP_INCLUDED__
#define __HEAP_INCLUDED__
#include <iostream>
#include <vector>
using namespace std;
class heap{
int capacity;
bool isMinHeap; //1 is min heap -- ascending order
vector<int> * content;
public:
heap();
heap(bool t);
heap(vector<int> * input);
heap(vector<int> * input, bool t);
void print();
void prettyPrint();
int parent(int i);
int leftChild(int i);
int rightChild(int i);
int size();
int getMax();
void insert(int data);
void heapifyDown(int index);
void heapifyUp(int index);
int invalidChild(int index);
int deleteMax();
int deleteMin();
bool minDir();
int at(int index);
};
vector<int> * heapSort(vector<int> * input);
void swap(vector<int> * vec, int a, int b);
#endif
这里是heap.cpp
中定义的构造函数。请注意,当我向该文件添加一个 main 以测试内容时,所有构造函数都可以正常工作:
class heap{
vector<int> * content;
int capacity = 256;
bool isMinHeap; //1 is min heap -- ascending order
public:
heap(){
content = new vector<int>;
isMinHeap = 0;
}
heap(bool t){
content = new vector<int>;
isMinHeap = t;
}
heap(vector<int> * input){
content = input;
isMinHeap = true;
for(int i = content->size()/2; i >= 0; i--){
heapifyDown(i);
}
}
heap(vector<int> * input, bool t){
content = input;
isMinHeap = t;
for(int i = content->size()/2; i >= 0; i--){
heapifyDown(i);
}
}
//other functions below
}
带有 bool 的构造函数在 main.cpp
中不起作用,#include "heap.h"
在顶部。这些文件都在同一目录中,我正在使用此命令进行编译:g++ heap.cpp main.cpp -o main
。为什么我的两个构造函数不起作用?
我看到的错误是
/usr/bin/ld: /tmp/ccwomODk.o: in function `main':
main.cpp:(.text+0x4e2): undefined reference to `heap::heap(bool)'
collect2: error: ld returned 1 exit status
-Wall
没有详细说明这个问题。我很确定问题出在我的链接上,因为当我在那里使用构造函数时,它们在 heap.cpp
内部工作。
您对 .cpp
文件中的 class 所做的操作是错误的。您不能定义 class 两次。程序中必须只有一个class heap { /*...*/ };
(但它可能包含在多个.cpp
文件中)。否则违反 one-definition-rule (ODR) 并且程序具有未定义的行为。
因此,请从 heap.cpp
中删除您显示的所有内容。
要在heap.cpp
文件中定义heap
的构造函数,需要使用这样的语法:
#include "heap.h"
heap::heap() {
/*...*/
}
heap::heap(bool t) {
/*...*/
}
//...
等等。其他成员函数必须以类似的方式定义,例如:
void heap::print() {
/*...*/
}
此外,如果你想有一个默认的成员初始值设定项,如
int capacity = 256;
将其添加到 .h
文件的声明中。
我还想补充一点,让 pointer-to-std::vector
作为成员几乎肯定也是一种错误的做法,但 out-of-scope 是个问题。
When you declare a program element such as a class, function, or variable, its name can only be "seen" and used in certain parts of your program. The context in which a name is visible is called its scope. For example, if you declare a variable x within a function, x is only visible within that function body.
看来你严重违反了 ODR 规则。您的 class 成员包括构造函数没有在源文件中声明主体 (heap.cpp)。 使用 '::' 使 class 成员拥有正文:
//heap.cpp
"heap.h"
heap::heap()
{
}
heap:heap(vector<int> * input, bool t)
{
}
int heap::parent(int i)
{
return i;
}
// this is how you create a body for function that are class members
// the same should be done for all other functions