【问题标题】:Unresolved External Symbol with a particular function in c++c++ 中具有特定功能的未解析外部符号
【发布时间】:2014-11-10 07:04:37
【问题描述】:

我收到如下错误消息:

calculatewinner Candidate 的未解决的外部符号错误

这是我的代码:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;

void headerforelection();
void getNamecalculatetotal(ifstream& in, string candidates[], double Votes[], double percentvotes[]);
void getNamecalculatetotal2(ifstream& in, string fname[], double Votes[], double percentvotes[]);
void allvotes(double []);
void Votesrecievedpercentage(ifstream& in, char Candidate[], double Votes[], string fname[], double percentvotes[]);
void Votesrecievedpercentage2(double Votes[], string fname[], double percentvotes[]);
void calculatewinner(string fname[], double Votes[]);
void headerforelection();

int main()
{

    ifstream in = ifstream();
    in.open("Votes.txt");
    ofstream out = ofstream();
    out.open("outputs.txt");

    char winner();
    char Candidate();
    string fname[5];
    double Votes[5];
    double percentvotes[5];
    double total = double();
    headerforelection();
    while (!in.eof())
    {


    getNamecalculatetotal(in, fname, Votes, percentvotes);


    Votesrecievedpercentage2(Votes, fname, percentvotes);
    }
    allvotes(Votes);
    calculatewinner(fname, Votes);

}

void getNamecalculatetotal(ifstream& in, string fname[], double Votes[], double percent[])
{

    double total = double();
        for (int i = 0; i < 5; i++)

        {

            in >> fname[i] >> Votes[i];

            total = total + Votes[i];
        }
        for (int j = 0; j < 5; j++)
        {
            percent[j] = (Votes[j] / total) * 100;
        }
    }
    void headerforelection()
    {
        std::cout << fixed << setfill(' ') << left << setw(10) << "Candidate"
            << right << setw(20) << setprecision(0) << "votes Recieved"
            << right << setw(20) << setprecision(2) << "% of Total Votes" << std::endl;
        std::cout << endl;
    }

    void Votesrecievedpercentage2( double Votes[], string fname[], double percentvotes[])

    {
        for (int b = 0; b < 5; b++)
        {
            std::cout << fixed << setfill(' ') << left << setw(10) << fname[b]
                << right << setw(16) << setprecision(0) << Votes[b]
                << right << setw(16) << setprecision(2) << percentvotes << std::endl;


        }
    }
    void allvotes(double Votes[])

    {
        double total = double();
        for (int i = 0; i < 5; i++)
        {
            total = total + Votes[i];
        }
        std::cout << setfill(' ') << left << setw(22) << "Total" << setprecision(0) << total << std::endl;
    }
    void calculatewinner(string fname[], double Votes[])
    {
        {
            int winner = 0;
            for (int l = 0; l, 5; l++)
            {
                if (Votes[l] > Votes[winner])
                {
                    winner = l;//3

                }
            }
        }
        char winner();
        char Candidate();

        std::cout << std::endl;
        std::cout << Candidate << "Is The Winner" << fname << "." << std::endl;
    }

【问题讨论】:

  • 你想用这条线做什么:char Candidate();?
  • ifstream in = ifstream(); 这种分配是完全错误的。要初始化输入流,请使用ifstream in("Votes.txt");,输出流也一样。

标签: c++ external symbols


【解决方案1】:

首先回答你的问题,改变这些行

char winner();
char Candidate();

您的代码中的所有位置(main 以及 calculatewinner)到以下内容:

char winner;
char Candidate;

通过添加括号,您实际上是在向函数声明原型。由于您从未定义函数Candidate(void),因此链接器会抱怨缺少实现。这也适用于char winner(),但由于您从不使用这个“变量”,链接器并不关心这个。

否则,您的代码将非常损坏。我敢肯定你只是在学习 C++,但你的代码在命名约定方面非常不一致,并且在做其他事情之前应该解决一些其他错误。

【讨论】:

    猜你喜欢
    • 2014-06-13
    • 2013-02-28
    • 2012-11-19
    • 1970-01-01
    • 2021-12-16
    • 2018-08-02
    • 2012-12-17
    • 2012-06-30
    相关资源
    最近更新 更多