#include "stack.h"
#include <string>
using namespace std;
int main() {
MyStack<int> thestack;
int input = -22222;
while (true) {
cout << "enter -999 to quit, Enter your binarty number: ";
cin >> input;
if (input == -999) { break; }
else {
string str = to_string(input);
thestack.convertBinaryToDecimal(str, thestack);
}
}
}
void convertBinaryToDecimal(string num, MyStack<int>& thestack) {
int count = 0;
string n = num;
int dec_value = 0;
int base = 1;
int length = num.length();
for (int i = length - 1; i >= 0; i--) {
if (num[i] == '1') {
dec_value += base;
base = base * 2;
thestack.push(base);
count++;
base = 1;
}
}
int value = 0;
while (!count==0) {
value += thestack.top();
thestack.pop();
}
cout << "The equiavlent decimal number is: " << value << endl;
}
.cpp文件^
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED
#include <iostream>
#include <string>
using namespace std;
template<class Type>
class MyStack {
public:
int count;
MyStack(int mysize = 100) {
list = new int[mysize];
maxStackSize = mysize;
stackTop = 0;
count = 0;
}
MyStack(const MyStack<Type>& thestack) {
copyStack(thestack);
}
~MyStack() {
delete[] list;
}
void initializeStack();
bool isEmptyStack() const;
bool isFullStack() const;
Type push(const Type& newItem);
Type top()const;
void pop();
void convertBinaryToDecimal(string num, MyStack<Type>& thestack);
const MyStack<Type>& operator=(const MyStack<Type>& thestack) {
copyStack(thestack);
return *(this);
}
private:
int maxStackSize;
int stackTop;
Type* list;
void copyStack(const MyStack<Type>&);
};
template<class Type>
void MyStack<Type>::initializeStack() {
stackTop = 0;
}
template<class Type>
bool MyStack<Type>::isEmptyStack() const {
return (stackTop == 0);
}
template<class Type>
bool MyStack<Type>::isFullStack() const {
return (stackTop == maxStackSize);
}
template<class Type>
Type MyStack<Type>::push(const Type& newItem) {
if (isFullStack()) {
return NULL;
}
list[stackTop] = newItem;
stackTop++;
return list[stackTop - 1];
}
template<class Type>
Type MyStack<Type>::top()const {
if (isEmptyStack()) {
return NULL;
}
return list[stackTop - 1];
}
template<class Type>
void MyStack<Type>::pop() {
if (isEmptyStack()) {
}
else {
stackTop--;
}
}
template<class Type>
void MyStack<Type>::copyStack(const MyStack<Type>& thestack) {
delete[] list;
list = new Type[thestack.maxStackSize];
for (int i = 0; i < thestack.maxStackSize; i++) {
list[i] = thestack.list[i];
}
stackTop = thestack.stackTop;
maxStackSize = thestack.maxStackSize;
}
#endif//STACK_H_INCLUDED
头文件^
我已经调试这个项目有一段时间了,只是四处移动并试图找出所有错误。我在这里碰到墙了,我想不通。
这是我得到的错误:
LNK2019无法解析的外部符号“ public:void __thiscall MyStack :: convertBinaryToDecimal(class std :: basic_string,class std :: allocator>,class MyStack&)”(?convertBinaryToDecimal @?$ MyStack @ H @@ QAEXV?$ basic_string函数_main中引用的@DU?$ char_traits @ D @ std @@ V?$ allocator @ D @ 2 @@ std @@ AAV1 @@ Z)