我把类的声明和定义,全部放在main函数所在的代码里了,但编译过程中还是出现undefined reference to (类的成员),查了各种方法,搞了一晚上,已经崩溃了。有大佬知道是什么问题吗。
cpp source:
using namespace std;
#include "ope_on_StackPair.h"
StackPair::StackPair() {
for (int & i : a)i = 0;
stack1.end = a;
stack2.end = a + NUM - 1;
stack1.length = stack2.length = 0;
}
struct stack StackPair::pop(struct stack sta) {
if (sta.length > 0)
sta.length -= 1;
return sta;
}
struct stack StackPair::push(struct stack sta , int con) {
if (stack1.length + stack2.length < NUM) {
if (sta.end == a) {
a[NUM - sta.length - 1] = con;
} else a[sta.length] = con;
sta.length += 1;
} else std::cout << "There's no spare space." << std::endl;
return sta;
}
int StackPair::inspect(struct stack sta) {
if (sta.length == 0) {
std::cout << "The stack is void. The returned is not true." << std::endl;
return 999999;
}
else {
if (sta.end == a)return a[sta.length - 1];
else return a[NUM - sta.length];
}
}
int StackPair::check(struct stack sta) {
if (sta.length == 0)return 1;
else return 0;
}
cpp header
using namespace std;
#include<iostream>
#define NUM 20 //The maximum length of the stack pair can be changed here, with default value of 20.
struct stack{ //'struct stack' defines the structure of the stack pair.
int* end;
int length;
};
class StackPair {
public:
static struct stack pop(struct stack sta);
static struct stack push(struct stack sta , int con);
static int inspect(struct stack sta);
static int check(struct stack);
StackPair();
static int a[NUM];
static struct stack stack1, stack2;
};
cmakelist:
cmake_minimum_required(VERSION 3.20)
project(stack_pair)
set(CMAKE_CXX_STANDARD 14)
add_executable(stack_pair main.cpp)
add_library(ope_on_StackPair ope_on_StackPair/ope_on_StackPair.cpp)
target_link_libraries(stack_pair ope_on_StackPair)