简单 class 的 LNK2019 错误
LNK2019 error with simple class
(抱歉,我知道有很多很多关于此错误的帖子,但其中 none 的程序似乎与我尝试构建的程序一样简单。抱歉,但我不知道我对 C++ 了解不够,无法弄清楚如何利用其他问题来发挥我的优势。)
我正在为我的 C++ 课程构建一个基于定义和初始化 class 的简单计算器程序。我认为我快完成了,但我一直收到错误消息:
Driver.obj : error LNK2019: unresolved external symbol "public: __thiscall >Calculator::Calculator(void)" (??0Calculator@@QAE@XZ) referenced in function >_main
我认为这与我的默认构造函数有关,但是有人可以帮我吗?我很感激我能得到的任何帮助!
代码如下:
Calculator.cpp
#include <string>
#include <iostream>
#include "Calculator.h"
using namespace std;
void Calculator::SetOperation(char oper, double opa, double opb)
{
Calculator::operation = oper;
Calculator::op1 = opa;
Calculator::op2 = opb;
}
void Calculator::Calc()
{
switch(operation)
{
case ('+'):
answer = op1 + op2;
result = "Your operation is addition: " + to_string(op1) + " + " + to_string(op2) + " = " + to_string(answer) + '\n';
break;
case ('-'):
answer = op1 - op2;
result = "Your operation is subtraction: " + to_string(op1) + " - " + to_string(op2) + " = " + to_string(answer) + '\n';
break;
case ('*'):
answer = op1 * op2;
result = "Your operation is multiplication: " + to_string(op1) + " * " + to_string(op2) + " = " + to_string(answer) + '\n';
break;
case ('/'):
answer = op1 / op2;
result = "Your operation is division: " + to_string(op1) + " / " + to_string(op2) + " = " + to_string(answer) + '\n';
break;
}
}
string Calculator::GetResults()
{
return result;
}
Calculator.h
#ifndef _CALCULATOR_H_
#define _CALCULATOR_H_
#include <string>
using namespace std;
class Calculator
{
private:
char operation;
double op1;
double op2;
double answer;
string result;
void Calc();
public:
Calculator();
void SetOperation(char oper, double opa, double opb);
string GetResults();
};
#endif
Driver.cpp
//
#include <string>
#include <iostream>
#include "Calculator.h"
#include "Functions.h"
using namespace std;
int main()
{
char op;
double numA, numB;
string ing, correct("no"), equals, another("no");
Header();
Calculator MyCalc;
do
{
do
{
cout<< "Enter the number of an operator from the following:\n\n"
<< "Addition (+): 1 Subtraction (-): 2\n"
<< "Multiplication (*): 3 Division (/): 4\n\n";
cin >> op;
cin.ignore();
switch(op)
{
case (1):
op = '+';
cout<< "\n\nLet's do some addition!\n\n";
ing = "adding";
break;
case (2):
op = '-';
cout<< "\n\nLet's do some subtraction!\n\n";
ing = "subtracting";
break;
case (3):
op = '*';
cout<< "\n\nLet's do some multiplication!\n\n";
ing = "multiplying";
break;
case (4):
op = '/';
cout<< "\n\nLet's do some division!\n\n";
ing = "dividing";
break;
}
cout<< "Please enter your first operand: ";
cin >> numA;
cin.ignore();
cout<< "\n\nPlease enter your second operand: ";
cin >> numB;
cin.ignore();
if (op == '/' && numB == 0)
{
cout<< "!!!!! ILLEGAL OPERATION\n!!!!! Can't divide by zero!";
correct = "no";
}
else
{
cout<< "We'll be " << ing << ' ' << numA << " and " << numB << ", correct?\n<yes/no>>> ";
getline(cin, correct);
}
} while(correct != "yes");
MyCalc.SetOperation(op, numA, numB);
equals = MyCalc.GetResults();
cout<< "\n\n" << equals;
cout<< "\n\n\nWould you like to perform another calculation?\n<yes/no>>>";
} while (another != "no");
}
Functions.h
#ifndef _FUNCTIONS_H_
#define _FUNCTIONS_H_
void Header();
#endif
Functions.cpp
#include <string>
#include <iostream>
#include "Functions.h"
using namespace std;
void Header()
{
do
{
cout<< "blah blah blah";
} while(cin.get() != '\n');
}
您已在 Calculator.h
中声明了构造函数,但尚未在 Calculator.cpp
(或其他任何地方)中定义它。
添加
Calculator::Calculator() { }
到 Calculator.cpp
将解决问题。
(抱歉,我知道有很多很多关于此错误的帖子,但其中 none 的程序似乎与我尝试构建的程序一样简单。抱歉,但我不知道我对 C++ 了解不够,无法弄清楚如何利用其他问题来发挥我的优势。)
我正在为我的 C++ 课程构建一个基于定义和初始化 class 的简单计算器程序。我认为我快完成了,但我一直收到错误消息:
Driver.obj : error LNK2019: unresolved external symbol "public: __thiscall >Calculator::Calculator(void)" (??0Calculator@@QAE@XZ) referenced in function >_main
我认为这与我的默认构造函数有关,但是有人可以帮我吗?我很感激我能得到的任何帮助!
代码如下:
Calculator.cpp
#include <string>
#include <iostream>
#include "Calculator.h"
using namespace std;
void Calculator::SetOperation(char oper, double opa, double opb)
{
Calculator::operation = oper;
Calculator::op1 = opa;
Calculator::op2 = opb;
}
void Calculator::Calc()
{
switch(operation)
{
case ('+'):
answer = op1 + op2;
result = "Your operation is addition: " + to_string(op1) + " + " + to_string(op2) + " = " + to_string(answer) + '\n';
break;
case ('-'):
answer = op1 - op2;
result = "Your operation is subtraction: " + to_string(op1) + " - " + to_string(op2) + " = " + to_string(answer) + '\n';
break;
case ('*'):
answer = op1 * op2;
result = "Your operation is multiplication: " + to_string(op1) + " * " + to_string(op2) + " = " + to_string(answer) + '\n';
break;
case ('/'):
answer = op1 / op2;
result = "Your operation is division: " + to_string(op1) + " / " + to_string(op2) + " = " + to_string(answer) + '\n';
break;
}
}
string Calculator::GetResults()
{
return result;
}
Calculator.h
#ifndef _CALCULATOR_H_
#define _CALCULATOR_H_
#include <string>
using namespace std;
class Calculator
{
private:
char operation;
double op1;
double op2;
double answer;
string result;
void Calc();
public:
Calculator();
void SetOperation(char oper, double opa, double opb);
string GetResults();
};
#endif
Driver.cpp
//
#include <string>
#include <iostream>
#include "Calculator.h"
#include "Functions.h"
using namespace std;
int main()
{
char op;
double numA, numB;
string ing, correct("no"), equals, another("no");
Header();
Calculator MyCalc;
do
{
do
{
cout<< "Enter the number of an operator from the following:\n\n"
<< "Addition (+): 1 Subtraction (-): 2\n"
<< "Multiplication (*): 3 Division (/): 4\n\n";
cin >> op;
cin.ignore();
switch(op)
{
case (1):
op = '+';
cout<< "\n\nLet's do some addition!\n\n";
ing = "adding";
break;
case (2):
op = '-';
cout<< "\n\nLet's do some subtraction!\n\n";
ing = "subtracting";
break;
case (3):
op = '*';
cout<< "\n\nLet's do some multiplication!\n\n";
ing = "multiplying";
break;
case (4):
op = '/';
cout<< "\n\nLet's do some division!\n\n";
ing = "dividing";
break;
}
cout<< "Please enter your first operand: ";
cin >> numA;
cin.ignore();
cout<< "\n\nPlease enter your second operand: ";
cin >> numB;
cin.ignore();
if (op == '/' && numB == 0)
{
cout<< "!!!!! ILLEGAL OPERATION\n!!!!! Can't divide by zero!";
correct = "no";
}
else
{
cout<< "We'll be " << ing << ' ' << numA << " and " << numB << ", correct?\n<yes/no>>> ";
getline(cin, correct);
}
} while(correct != "yes");
MyCalc.SetOperation(op, numA, numB);
equals = MyCalc.GetResults();
cout<< "\n\n" << equals;
cout<< "\n\n\nWould you like to perform another calculation?\n<yes/no>>>";
} while (another != "no");
}
Functions.h
#ifndef _FUNCTIONS_H_
#define _FUNCTIONS_H_
void Header();
#endif
Functions.cpp
#include <string>
#include <iostream>
#include "Functions.h"
using namespace std;
void Header()
{
do
{
cout<< "blah blah blah";
} while(cin.get() != '\n');
}
您已在 Calculator.h
中声明了构造函数,但尚未在 Calculator.cpp
(或其他任何地方)中定义它。
添加
Calculator::Calculator() { }
到 Calculator.cpp
将解决问题。