【问题标题】:Linker error undefined reference to (all of my defined functions when called)链接器错误未定义引用(调用时我定义的所有函数)
【发布时间】:2012-05-30 16:10:38
【问题描述】:

几天前我写了这个程序作为一种概念验证。现在我知道它有效,我正在尝试清理代码并使事情变得更好。与我编写的原始程序和它的新版本相比,最大的变化是我将不止一次使用的任何东西变成了一个函数。我遇到的问题是对于我调用任何函数的每个地方,我都会收到链接器错误。除了这个错误,我相信程序现在已经完成了。

最棘手的一点是昨晚——在我遇到这个错误并处理与char*和char有关的另一个错误之前...——我设法修复了我的错误和我的代码工作得很好,即使是当时编写的一个函数。当我今天开始研究它时,我不知何故发现我不知何故失去了那个进步并去重新修复它。一旦我再次修复了错误——据我所知,我以完全相同的方式进行了修复——每次我调用该函数以及每个新函数在我编写它们并在main() 中调用它们时都会出现此链接器错误.

代码如下:

//******************************************************************************
// David Ewing
// This program is an improvement on the original Cypher program which cyphered
// upto 255 characters of inputed text according to a provided keyphrase. This
// version seeks to improve the flow of the code and allow for a larger input of
// text.
//******************************************************************************

#include <iostream>

using namespace std;

// Declair global variables
char keyphrase[8192];
char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
char cypherAlphabet[8192];
char sourceText[8192];
char cypherText[8192];
int sizeKeyphrase;
int sizeCypherAlphabet;
int sizeSourceText;
// ...Counters
int counter1;
int counter2;
int counter3;

// Declair functions
void cypher (char, int);
void eliminateDuplicates (char, int);
int getSize(char);
void handleSpaces (char, bool);
void reduceCase (char, int);

//******************************************************************************
// MAIN
//******************************************************************************

int main( void )
{
    // Describe program
    cout << "This program cyphers whatever text the user wishes according to a " << endl;
    cout << "keyphrase that the user provides." << endl << endl;

    // Retrieve keyphrase
    cout << "Please enter the keyphrase that you would like to use for your cypher." << endl;
    cout << "The keyphrase may include spaces, repeated letters, and capitals, but " << endl;
    cout << "they will be removed before it is applied to the provided text." << endl;
    cout << "Enter \"DONE\" when you have finished typing your keyphrase." << endl;

    // Call function to remove spaces from keyphrase
    handleSpaces (keyphrase[8192], false);

    // Call function to get size of keyphrase
    sizeKeyphrase = getSize (keyphrase[8192]);

    // Call function to remove repeated letters from keyphrase
    eliminateDuplicates (keyphrase[8192], sizeKeyphrase);

    // Call function to normalize case of keyphrase
    reduceCase (keyphrase[8192], sizeKeyphrase);

    // Apply keyphrase to alphabet
    strcpy (cypherAlphabet, keyphrase); // Copy the keyphrase to the beginning of cypherAlphabet
    strcat (cypherAlphabet, alphabet); // Add the rest of the alphabet after the keyphrase to cypherAlphabet

    // Call function to get size of cypherAlphabet
    sizeCypherAlphabet = getSize (cypherAlphabet[8192]);

    // Call function to remove repeated letters from cypherAlphabet
    eliminateDuplicates(cypherAlphabet[8192], sizeCypherAlphabet);

    // Retrieve sourceText
    cout << endl << "Please enter the text which you wish to be cyphered. Capitals will be" << endl;
    cout << "removed but punctuation and any other non-alphanumeric characters will" << endl;
    cout << "be ignored." << endl;
    cout << "Enter \"DONE\" when you have finished typing your text." << endl;

    // Call function to take input for sourceText and to handle spaces
    handleSpaces(sourceText[8192], true);

    // Call function to get size of sourceText
    sizeSourceText = getSize (sourceText[8192]);

    // Call function to normalize case of sourceText
    reduceCase(sourceText[8192], sizeSourceText);

    // Cypher sourceText
    cypher(sourceText[8192], sizeSourceText);

    // Display cypherText
    cout << endl << "Your cyphered text is as follows:" << endl;
    cout << cypherText << endl << endl;

    // Pause program
    system ("pause");

    // End program
    return 0;

} // End main

//******************************************************************************
// CYPHER
//******************************************************************************
void cypher (char text[8192], int size)
    {
         // Declare counters
         counter1 = 0;
         counter2 = 0;

     // Search alphabet for address of each letter
     while (counter1 < size)
     {
           counter2++;
           if (sourceText[counter1] == alphabet[counter2-1])
           {
                             cypherText[counter1] = cypherAlphabet[counter2 - 1];
                             counter1++;
                             counter2 = 0;
           } // End if

           if (counter2 > 25) // If all the letters in the alphabet are checked
           {
                 cypherText[counter1] = sourceText[counter1]; // give up on the letter, transcribing it over
                 counter1++; // and continue with the rest of the soureText
                 counter2 = 0; // Allows for punctuation, spaces, strange symbols, etc.
           } // End if
     } // End while

} // End cypher

//******************************************************************************
// ELIMINATE DUPLICATES
//******************************************************************************
void eliminateDuplicates (char text[8192], int size)
{
     // Initialize counters
     counter1 = 0;
     counter2 = 1;
     counter3 = 1;

     // Initialize flag
     //bool flag = false;

     while (counter1 < size)
    {
          if (text[counter1] == text[counter1+counter2] && text[counter1] != NULL)
          {
                         // Delete text[i+j]
                         for (counter3 = 1; counter3 < size; counter3++)
                         {
                             text[counter1+counter2+counter3-1] = text[counter1+counter2+counter3]; // Shift array left at repeated letter. Final value is doubled.
                             //if (counter3 == size - 1)
                             //{
                             //      flag = true;
                             //} // End if
                         } // End for
          } // End if
          else if (counter2 == size)
          {
              counter1++;
              counter2 = 1; // Reset counter
              //flag = false; // Reset flag
          } // End else if
          else
          {
              counter2++;
              //flag = false;
          } // End else

    } // End while

} // End eliminateDuplicates

//******************************************************************************
// GET SIZE
//******************************************************************************
int getSize (char text[8192])
{
    int size = 0; // Declair counter/result variable
    while (text[size] != NULL)
    {
          size++;
    } // End while

    return size;

} // End getSize

//******************************************************************************
// HANDLE SPACES
//******************************************************************************
void handleSpaces (char text[8192], bool includeSpaces)
{
     // Declare temporary input holder
     char temp[8192];

     // Initialize flag
     bool flag = false;

     cin >> text;
     while (flag == false)
     {
           cin >> temp;
           if (strcmp(temp, "DONE"))
           {
                            if (includeSpaces == true)
                            {
                                              strcat(text, " "); // Add space after last word
                            } // End if

                            strcat(text, temp); // Add the next word to the last

           } // End if
           else
           {
               flag = true;
           } // End else

     } // End while

} // End handleSpaces

//******************************************************************************
// REDUCE CASE
//******************************************************************************
void reduceCase (char text[8192], int size)
{
     // Declare counter
     int counter1 = 0;
     while (counter1 < size)
     {
           sourceText[counter1] = tolower(sourceText[counter1]); // Use tolower for each item in the array
           counter1++;
           } // End while

} // End reduceCase

【问题讨论】:

    标签: c++ function reference linker call


    【解决方案1】:

    声明和定义不同:

    声明:

    void cypher (char, int);
    

    定义:

    void cypher (char text[8192], int size)
    

    只是一个例子。

    另外,调用错误:

    cypher(sourceText, sizeSourceText);
    

    而不是

    cypher(sourceText[8192], sizeSourceText);
    

    当你写作时:

    void cypher (char text[8192], int size)
    

    表示函数以数组为参数。

    当你调用它时

    cypher(sourceText[8192], sizeSourceText);
    

    这意味着您正在使用来自该 char 数组的位置 8192 的字符调用函数。

    【讨论】:

    • 我之前提到的错误是“从 char* 到 char 的无效转换”。如果我将该函数称为:cypher(sourceText, sizeSourceText);,我会再次收到该错误。同样,如果我将函数声明为:void cypher (char[8192], int); 我会收到该错误。 ...从头开始。如果我进行这两项更改,错误就会消失。一切似乎都在工作。非常感谢!另外,感谢您闪电般的快速响应。我没想到今天会得到任何东西!
    • @Davidlewing SO 的力量很强。当然这两个变化:P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    相关资源
    最近更新 更多