使用头文件中包含的函数时未定义的引用

use*_*526 3 c++ codeblocks

我在使用我的c ++源文件或者编译器本身时遇到了一些奇怪的事情.似乎当我尝试编译文件时,它会给我发一条消息 -

未定义的引用"Basic_int_stack :: Basic_int_stack()

未定义的引用"Basic_int_stack :: Push(int)

这是我的代码(我还是初学者,所以不要指望任何疯狂的专业代码)

头文件:

class Basic_int_stack
{
  public:
    // This part should be implementation independent.
    Basic_int_stack(); // constructor
    void push( int item );
    int pop();
    int top();
    int size();
    bool empty();

  private:
    // This part is implementation-dependant.
    static const int capacity = 10 ; // the array size
    int A[capacity] ; // the array.
    int top_index ; // this will index the top of the stack in the array
};
Run Code Online (Sandbox Code Playgroud)

实现:

#include "basic_int_stack.h"// contains the declarations of the variables and functions.

Basic_int_stack::Basic_int_stack(){
  // the default constructor intitializes the private variables.
  top_index = -1; // top_index == -1 indicates the stack is empty.
}

void Basic_int_stack::push( int item ){
  top_index = top_index + 1;
  A[top_index] = item ;
}

int Basic_int_stack::top(){
  return A[top_index];
}

int Basic_int_stack::pop(){
  top_index = top_index - 1 ;
  return A[ top_index + 1 ];
}


bool Basic_int_stack::empty(){
  return top_index == -1 ;
}

int Basic_int_stack::size(){
    return top_index;
}
Run Code Online (Sandbox Code Playgroud)

主功能:

#include "basic_int_stack.h"
#include <iostream>


int main()
{
    int var;

    Basic_int_stack s1;
    while((std::cin >> var)>=0){
        s1.push(var);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Joh*_*nck 7

发生这种情况是因为您正在构建主文件而不构建和链接类实现文件.您需要以某种方式调整构建设置.