简体   繁体   中英

Unresolved External Symbol with a particular function in c++

I got the error message like the following:

Unresolved External Symbol error with calculatewinner Candidate

Here is my code:

#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;
    }

First to answer your question, change these lines

char winner();
char Candidate();

everywhere in your code (in main as well as in calculatewinner ) to the following:

char winner;
char Candidate;

By adding the paranthesis you are actually declaring a prototype to a function. Since you never define a function Candidate(void) the linker complains about the missing implementation. This would apply to char winner() also, but since you never use this "variable", the linker isn't concerned about this one.

Otherwise, your code is very broken. I am sure you are just learning C++, but your code is quite inconsistent in naming conventions as well as some other errors that should be addressed before doing something else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM