2015-06-10 45 views
1

(對不起,我知道有這個錯誤很多,很多帖子,但沒有人似乎有簡單的程序,因爲我試圖建立。我很抱歉,但我不有足夠的瞭解C++弄清楚如何使用等問題,我的優勢。)LNK2019錯誤與簡單的類

我建立一個基於定義和初始化類爲我的C++當然一個簡單的計算器程序。我認爲我接近完成,但我不斷收到一個錯誤:

Driver.obj : error LNK2019: unresolved external symbol "public: __thiscall >Calculator::Calculator(void)" ([email protected]@[email protected]) 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'); 
} 

回答

2

你已經宣佈Calculator.h的構造函數,但還沒有(別的或任何)Calculator.cpp定義它。

添加

Calculator::Calculator() { } 

Calculator.cpp將解決這個問題。

+0

如果可以的話,我會投票,這正是我需要的,但其他發佈的問題太難以用我有限的編程知識來解析。謝謝! – ThatGuy01001010