C ++错误:错误LNK2019:未解析的外部符号

时间:2013-02-26 01:33:04

标签: c++ class linker

我查看了其他包含这些错误的问题。我无法从这些问题中找到解决方案。一些答案说某些功能没有实现,但我的功能是。当我尝试从VS2012命令行运行程序时,我只得到标题错误。当我从IDE运行程序时,我没有错误,一切都很好。

Visual Studio 2012项目

Imgur

useTTH.cpp

#include <iostream>
#include <string>
#include "tth.h"
using namespace std;

int main()
{
    string message;
    string choice;

    do
    {
        cout << "Please enter a message: ";
        getline(cin, message);

        tth tthObject;
        tthObject.getMessage(message);
        tthObject.encryptMessageOnlyLetters();

        cout << endl;
        cout << "Encrypt another message? (Yes/No)" << endl;
        cout << "> ";
        getline(cin, choice);
        cout << endl;
    }
    while(choice.substr(0, 1) == "Y" || choice.substr(0, 1) == "y");

    system("PAUSE");
    return 0;
}

tth.h

#include <string>
#include <vector>
using namespace std;

class tth
{
    public:
        tth();
        void getMessage(string messageP);
        void setMessageOnlyLettersNumberOf16Blocks();
        void encryptMessageOnlyLetters();
        void messageOnlyLettersToFourByFourLetters();
        void matchLetterToNumber();
        void calculateAndShowRunningTotalFourByFourNumbers();
        void shiftRowsAccordingly();
        void calculateAndShowRunningTotalTemp();
        void matchNumberToLetter();
        void showFourLetterHashNumbers();


    private:
        string message;
        vector<char> messageOnlyLetters;
        int runningTotal[4];
        char fourLetterHash[4];
        int messageLength;
        int messageOnlyLettersNumberOf16Blocks;
        char fourByFourLetters[4][4];
        int fourByFourNumbers[4][4];
        int temp[4][4];
};

tth.cpp

#include <iostream>
#include <string>
#include <cctype>
#include "tth.h"

//--------------------------------------------------------------------------------------------------------------
// tth()
//--------------------------------------------------------------------------------------------------------------
// Constructor for the tth class.
// Initializes the elements of the private integer array 'runningTotal' to 0.
// Initializes the elements of the private char array 'fourLetterHash' to 'A'.
//--------------------------------------------------------------------------------------------------------------
tth::tth()
{
    for(int i = 0; i < 4; i++)
    {
        runningTotal[i] = 0;
        fourLetterHash[i] = 'A';
    }
}

//--------------------------------------------------------------------------------------------------------------
// getMessage(string messageP)
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Sets the value of the private string variable 'message' to the value of the string 'messageP' parameter.
// Gives the private char vector 'messageOnlyLetters' only letter elements of the 'message' variable. 
//--------------------------------------------------------------------------------------------------------------
void tth::getMessage(string messageP)
{
    message = messageP;
    messageLength = message.length();

    for(int i = 0; i < messageLength; i++)
    {
        if(isalpha(message[i]))
        {
            messageOnlyLetters.push_back(message[i]);
        }
    }
}

//--------------------------------------------------------------------------------------------------------------
// setMessageOnlyLettersNumberOf16Blocks()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Pads the messageOnlyLetters vector with a number 'A' elements equal to modulo 16 of the size of the vector.
// Sets the private int variable 'messageOnlyLettersNumberOf16Blocks' to the quotient of the vector size and 16.
//--------------------------------------------------------------------------------------------------------------
void tth::setMessageOnlyLettersNumberOf16Blocks()
{
    int messageOnlyLettersModulo = messageOnlyLetters.size() % 16;

    while(messageOnlyLettersModulo != 0)
    {
        messageOnlyLetters.push_back('A');
        messageOnlyLettersModulo = messageOnlyLetters.size() % 16;
    }

    messageOnlyLettersNumberOf16Blocks = messageOnlyLetters.size() / 16;
}

//--------------------------------------------------------------------------------------------------------------
// encryptMessageOnlyLetters()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Executes the 'setMessageOnlyLettersNumberOf16Blocks()' once.
// Executes other functions of the tth class a number of times equal to 'messageOnlyLettersNumberOf16Blocks'.
//--------------------------------------------------------------------------------------------------------------
void tth::encryptMessageOnlyLetters()
{
    setMessageOnlyLettersNumberOf16Blocks();

    for (int i = 0; i < messageOnlyLettersNumberOf16Blocks; i++)
    {
        messageOnlyLettersToFourByFourLetters();
        matchLetterToNumber();
        calculateAndShowRunningTotalFourByFourNumbers();
        cout << endl;
        shiftRowsAccordingly();
        calculateAndShowRunningTotalTemp();
        cout << endl;
        matchNumberToLetter();
        showFourLetterHashNumbers();
        cout << endl;
    }
}

//--------------------------------------------------------------------------------------------------------------
// messageOnlyLettersToFourByFourLetters()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Sets the elements of the private char 2D array 'fourByFourLetters' to the elements of 'messageOnlyLetters'.
// Reduces the size of a non-empty messageOnlyLetters by 16 elements.
//--------------------------------------------------------------------------------------------------------------
void tth::messageOnlyLettersToFourByFourLetters()
{
    int count = 0;

    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            fourByFourLetters[i][j] = messageOnlyLetters[count];
            count++;
        }
    }

    if(!(messageOnlyLetters.empty()))
    {
        messageOnlyLetters.erase(messageOnlyLetters.begin(), messageOnlyLetters.begin() + 16);
    }
}

//--------------------------------------------------------------------------------------------------------------
// matchLetterToNumber()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Sets the elements of the private int 2D array 'fourByFourNumbers' to a number 0 - 25.
// The number is determined by the ASCII value of a 'fourByFourLetters' element.
//--------------------------------------------------------------------------------------------------------------
void tth::matchLetterToNumber()
{
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            switch(static_cast<int>(fourByFourLetters[i][j]))
            {
                case 65:
                case 97:
                    fourByFourNumbers[i][j] = 0;
                    break;
                case 66:
                case 98:
                    fourByFourNumbers[i][j] = 1;
                    break;
                case 67:
                case 99:
                    fourByFourNumbers[i][j] = 2;
                    break;
                case 68:
                case 100:
                    fourByFourNumbers[i][j] = 3;
                    break;
                case 69:
                case 101:
                    fourByFourNumbers[i][j] = 4;
                    break;
                case 70:
                case 102:
                    fourByFourNumbers[i][j] = 5;
                    break;
                case 71:
                case 103:
                    fourByFourNumbers[i][j] = 6;
                    break;
                case 72:
                case 104:
                    fourByFourNumbers[i][j] = 7;
                    break;
                case 73:
                case 105:
                    fourByFourNumbers[i][j] = 8;
                    break;
                case 74:
                case 106:
                    fourByFourNumbers[i][j] = 9;
                    break;
                case 75:
                case 107:
                    fourByFourNumbers[i][j] = 10;
                    break;
                case 76:
                case 108:
                    fourByFourNumbers[i][j] = 11;
                    break;
                case 77:
                case 109:
                    fourByFourNumbers[i][j] = 12;
                    break;
                case 78:
                case 110:
                    fourByFourNumbers[i][j] = 13;
                    break;
                case 79:
                case 111:
                    fourByFourNumbers[i][j] = 14;
                    break;
                case 80:
                case 112:
                    fourByFourNumbers[i][j] = 15;
                    break;
                case 81:
                case 113:
                    fourByFourNumbers[i][j] = 16;
                    break;
                case 82:
                case 114:
                    fourByFourNumbers[i][j] = 17;
                    break;
                case 83:
                case 115:
                    fourByFourNumbers[i][j] = 18;
                    break;
                case 84:
                case 116:
                    fourByFourNumbers[i][j] = 19;
                    break;
                case 85:
                case 117:
                    fourByFourNumbers[i][j] = 20;
                    break;
                case 86:
                case 118:
                    fourByFourNumbers[i][j] = 21;
                    break;
                case 87:
                case 119:
                    fourByFourNumbers[i][j] = 22;
                    break;
                case 88:
                case 120:
                    fourByFourNumbers[i][j] = 23;
                    break;
                case 89:
                case 121:
                    fourByFourNumbers[i][j] = 24;
                    break;
                case 90:
                case 122:
                    fourByFourNumbers[i][j] = 25;
                    break;
                default:
                    cout << "Hmmmm";
            }
        }
    }
}

//--------------------------------------------------------------------------------------------------------------
// calculateAndShowRunningTotalFourByFourNumbers()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Calculates and sets the elements of 'runningTotal' using an accumulator with 'fourByFourNumbers'.
// Displays a command line message with each element of 'runningTotal'.
//--------------------------------------------------------------------------------------------------------------
void tth::calculateAndShowRunningTotalFourByFourNumbers()
{
    cout << endl;
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            runningTotal[i] = runningTotal[i] + fourByFourNumbers[j][i];
        }

        runningTotal[i] = runningTotal[i] % 26;
        cout << runningTotal[i] << " ";
    }
}

//--------------------------------------------------------------------------------------------------------------
// shiftRowsAccordingly()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Sets the elements of the private int 2D array 'temp' to a rearranged version of 'fourByFourNumbers'.
// Elements of rows 1 - 3 are shifted in a circular pattern.
// Row 1 elements are shifted to the left by 1 index. Row 2 elements are shifted to the left by 2 indicies.
// Row 3 elements are shifted to the left by 3 indicies. Row 4 elements are swapped: 1 <> 4 and 2 <> 3.
//--------------------------------------------------------------------------------------------------------------
void tth::shiftRowsAccordingly()
{
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            if(i == 0)
            {
                if(j == 3)
                {
                    temp[i][j] = fourByFourNumbers[i][j - 3];
                }
                else
                {
                    temp[i][j] = fourByFourNumbers[i][j + 1];
                }
            }
            else if(i == 1)
            {
                if(j == 2)
                {
                    temp[i][j] = fourByFourNumbers[i][j - 2];
                }
                else if(j == 3)
                {
                    temp[i][j] = fourByFourNumbers[i][j - 2];
                }
                else
                {
                    temp[i][j] = fourByFourNumbers[i][j + 2];
                }
            }
            else if(i == 2)
            {
                if(j == 1)
                {
                    temp[i][j] = fourByFourNumbers[i][j - 1];
                }
                else if(j == 2)
                {
                    temp[i][j] = fourByFourNumbers[i][j - 1];
                }
                else if(j == 3)
                {
                    temp[i][j] = fourByFourNumbers[i][j - 1];
                }
                else
                {
                    temp[i][j] = fourByFourNumbers[i][j + 3];
                }
            }
            else
            {
                if(j == 1)
                {
                    temp[i][j] = fourByFourNumbers[i][j + 1];
                }
                else if(j == 2)
                {
                    temp[i][j] = fourByFourNumbers[i][j - 1];
                }
                else if(j == 3)
                {
                    temp[i][j] = fourByFourNumbers[i][j - 3];
                }
                else
                {
                    temp[i][j] = fourByFourNumbers[i][j + 3];
                }
            }
        }
    }
}

//--------------------------------------------------------------------------------------------------------------
// calculateAndShowRunningTotalTemp()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Calculates and sets the elements of 'runningTotal' using an accumulator with 'temp'.
// Displays a command line message with each element of 'runningTotal'.
//--------------------------------------------------------------------------------------------------------------
void tth::calculateAndShowRunningTotalTemp()
{
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            runningTotal[i] = runningTotal[i] + temp[j][i];
        }

        runningTotal[i] = runningTotal[i] % 26;
        cout << runningTotal[i] << " ";
    }
}

//--------------------------------------------------------------------------------------------------------------
// matchNumberToLetter()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Sets the elements of the private char 2D array 'fourLetterHash' to a letter A - Z.
// The letter is determined by the value of a 'runningTotal' element.
//--------------------------------------------------------------------------------------------------------------
void tth::matchNumberToLetter()
{
    for(int i = 0; i < 4; i++)
    {
        switch(runningTotal[i])
        {
            case 0:
                fourLetterHash[i] = 'A';
                break;
            case 1:
                fourLetterHash[i] = 'B';
                break;
            case 2:
                fourLetterHash[i] = 'C';
                break;
            case 3:
                fourLetterHash[i] = 'D';
                break;
            case 4:
                fourLetterHash[i] = 'E';
                break;
            case 5:
                fourLetterHash[i] = 'F';
                break;
            case 6:
                fourLetterHash[i] = 'G';
                break;
            case 7:
                fourLetterHash[i] = 'H';
                break;
            case 8:
                fourLetterHash[i] = 'I';
                break;
            case 9:
                fourLetterHash[i] = 'J';
                break;
            case 10:
                fourLetterHash[i] = 'K';
                break;
            case 11:
                fourLetterHash[i] = 'L';
                break;
            case 12:
                fourLetterHash[i] = 'M';
                break;
            case 13:
                fourLetterHash[i] = 'N';
                break;
            case 14:
                fourLetterHash[i] = 'O';
                break;
            case 15:
                fourLetterHash[i] = 'P';
                break;
            case 16:
                fourLetterHash[i] = 'Q';
                break;
            case 17:
                fourLetterHash[i] = 'R';
                break;
            case 18:
                fourLetterHash[i] = 'S';
                break;
            case 19:
                fourLetterHash[i] = 'T';
                break;
            case 20:
                fourLetterHash[i] = 'U';
                break;
            case 21:
                fourLetterHash[i] = 'V';
                break;
            case 22:
                fourLetterHash[i] = 'W';
                break;
            case 23:
                fourLetterHash[i] = 'X';
                break;
            case 24:
                fourLetterHash[i] = 'Y';
                break;
            case 25:
                fourLetterHash[i] = 'Z';
                break;
            default:
                cout << "Hmmmm";
        }
    }
}

//--------------------------------------------------------------------------------------------------------------
// showFourLetterHashNumbers()
//--------------------------------------------------------------------------------------------------------------
// A public function of the tth class.
// Displays a command line message with each element of 'fourLetterHash'.
//--------------------------------------------------------------------------------------------------------------
void tth::showFourLetterHashNumbers()
{
    for(int i = 0; i < 4; i++)
    {
        cout << fourLetterHash[i] << " ";
    }
}

修改

尔加。我在这篇文章中花了很多精力才发现这是一个简单的命令行编译问题。谢谢你的回答。

答案

使用以下命令编译定义文件和主程序文件。

cl tth.cpp useTTH.cpp

2 个答案:

答案 0 :(得分:1)

您忘记在编译器命令中包含所有源文件。由于未包含tth.cpp,因此无法找到与其中定义的函数/变量对应的符号。

答案 1 :(得分:1)

enter image description here您应该同时编译tth.cppuseTTH.cpp

使用命令行:

cl tth.cpp useTTH.cpp