m0_54115846 2021-10-07 19:06 采纳率: 0%
浏览 56

写c++程序时,g++出现错误提示undefined reference to

我把类的声明和定义,全部放在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)

  • 写回答

3条回答 默认 最新

  • [PE]经典八炮 2021-10-07 19:07
    关注

    代码呢

    评论

报告相同问题?

问题事件

  • 修改了问题 10月7日
  • 创建了问题 10月7日

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题