C ++程序称为“Palindrome整数”两个函数。功能1 //返回反转整数。函数2 //如果是回文,则返回true

时间:2013-12-08 01:21:44

标签: function visual-c++ boolean palindrome

请帮助

我正在使用Visual Basic 2010在C ++中编写程序,该程序被称为“Palindrome整数”我需要编写两个函数//返回整数的反转。例如,反向(456)返回654 //带标题: - >

int reverse(int number)

我需要编写另一个函数//如果number是回文,则返回true //带标题: - >

bool isPalindrome(int number)

我需要使用反向函数来实现函数isPalindrome。如果数字反转与自身相同,则数字是回文。我的程序应报告该数字是否为回文。一切都在一个文件中。

我认为这个程序在我第一次编写代码时不是两个函数而是直接编写到int main()中。但我必须将代码放入指定的两个函数中,一旦我完成了并进行了调整,我得到了以下错误消息,并且没有出现黑色cout显示框。这是错误报告的片段,后面是完整的错误报告

:错误LNK2005:已在Driver.obj中定义的“int __cdecl reverse(int)”(?reverse @@ YAHH @ Z)

:致命错误LNK1169:找到一个或多个多重定义的符号

我收到以下错误报告

1>------ Build started: Project: Palindrome integer, Configuration: Debug Win32 ------
1>Build started 12/7/2013 4:54:25 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\Palindrome integer.unsuccessfulbuild".
1>ClCompile:
1>  All outputs are up-to-date.
1>ManifestResourceCompile:
1>  All outputs are up-to-date.
1>implementation.obj : error LNK2005: "int __cdecl reverse(int)" (?reverse@@YAHH@Z) already defined in Driver.obj
1>c:\documents and settings\dell\my documents\visual studio 2010\Projects\Palindrome integer\Debug\Palindrome integer.exe : fatal error LNK1169: one or more multiply defined symbols found
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:02.25
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

// Bellow是我的代码

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

//Retun reversal of an integer
int reverse(int number);              //function prototype

//Return true if number is a palindrome
bool isPalindrome(int number);            //function prototype


//Driver
int main()
{
    int usersNumber = 456; //0;    //a few lines commented tempararily for easier number testing
//  cout<<"Enter a number and I'll tell you if it's a Palindrome: ";
//  cin>> usersNumber;

bool palindromeToF = (isPalindrome(usersNumber));

    if (palindromeToF == true)
    {
        cout <<"YES the number is a Palindrome";
    }
    else
    {
        cout <<"NO the number is not a Palindrome";
    }


    return 0;
}

//function Implementation
//Retun reversal of an integer
int reverse(int number)
{
    //do while loop to count number of digits in Number
    int digitsCount = 0;
    double exponent1 = 1.0;
    int quotient;

    do
    {
        int tenToPower = pow( 10.0, exponent1);
    //  cout <<"tenToPower  "<< tenToPower <<"\t  ";
        quotient = (number / tenToPower);
    //      cout <<"exponent1  "<< exponent1<<"\t  ";
            exponent1++;

            //cout <<"quotient  "<< quotient<< "\t  "<<endl;
        digitsCount++;
    }while (!quotient == 0);


    //populating array "arrDigits" with integer's digits
    int *arrDigits = NULL;
    arrDigits = new int[digitsCount];
    double exponent2 = 0.0;
        for(int i = 0; i < digitsCount; i++)
        {
        int powerOfTen = pow( 10.0, exponent2);
    //cout <<endl<<"adding "<<((number / powerOfTen) % 10) <<" to sum";
        //cout <<powerOfTen;
        arrDigits[i]= ((number / powerOfTen) % 10);
        exponent2++;
        }

        //reverse number & populate array "arrDigRevers" with reversed order number 
        int *arrDigRevers = NULL;
        arrDigRevers = new int[digitsCount];
        int j = 0;
        int reversedNum = 0;
        double exponent3 = 0.0;
        for(int i = digitsCount-1; i >= 0; i--)
        {
            int powerOfTenB = pow( 10.0, exponent3);
        reversedNum += (powerOfTenB * arrDigits[i]);    //return of reverse func.
        exponent3++;

        /*   //reversed integer put into array 
        if(j < digitsCount)
        {
            arrDigRevers[j] = arrDigits[i];
            //cout <<"\t"<< "arrDigRevers"<<"["<< j<<"]="<< arrDigRevers[j]<<" "<< "arrDigits"<<"["<< j<<"]="<< " "<<arrDigits[j]<<" ";
            j++;
        }
        */

        }

        delete[] arrDigits;
        delete[] arrDigRevers;
        arrDigits = NULL;
        arrDigRevers = NULL;

        //cout <<endl<<"reversed number is "<< reversedNum;

        return reversedNum;
}


//function Implementation 
//Return true if number is a palindrome
bool isPalindrome(int number)
{
    if(number == reverse(number))
        {
            return true;
        }
        else
        {
            return false;
        }
}

1 个答案:

答案 0 :(得分:0)

答案就在这里:

  

1>implementation.obj : error LNK2005: "int __cdecl reverse(int)" (?reverse@@YAHH@Z) already defined in Driver.obj

似乎已在implementation.cpp和Driver.cpp中定义了int reverse(int number)。您需要从项目中重命名或删除其中一个定义。