所以,我删除并重新输入了错误引用的行,关闭并重新打开了visual studio,并查看了这里发布的几个与我的相同/相似措辞的错误。
我感觉这是视觉工作室的一个错误,因为即使在我删除了所有代码,保存和重新编译的内容之后,它也会在不再存在的同一行上出错。
“IntelliSense:类型为”void“的值无法分配给”double“类型的实体”
为了防止它与工作室的错误,我想我会问我的代码中可能导致此错误的任何想法?代码中间的blockquote是它引用该错误的行。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main ()
{
//Declarations of vectors and basic holders for input transfer
vector<string> candidateName;
string inputString;
vector<int> candidateVotes;
int counter,inputInt,totaledVotes;
double percentage;
vector<double> candidatePercentage;
//Method declaration for calculations
void calculatePercentage (int, int, int);
//User input to gather number of candidates, names, and votes received.
cout <<"How many candidates need to be input?";
cin>>counter;
for(int i = 0;i<counter;i++)
{
cout<<"Please enter the candidate's last name.";
cin>>inputString;
candidateName.push_back(inputString);
cout<<"Please enter the number of votes "<<candidateName[i]<<" received.";
cin>>inputInt;
candidateVotes.push_back(inputInt);
totaledVotes+=candidateVotes[i];
}
for(int i = 0;i<counter;i++)
{
//Problem here vvv
percentage = calculatePercentage(totaledVotes, candidateVotes[i], i);
//Problem there ^^^
candidatePercentage.push_back(percentage);
}
}
double calculatePercentage (int totalVotes, int candidateVotes, int rosterNumber)
{
int percentage;
percentage = candidateVotes/totalVotes;
percentage*=100;
return percentage;
}
答案 0 :(得分:8)
你有这个声明
void calculatePercentage (int, int, int);
然后你做
percentage = calculatePercentage(totaledVotes, candidateVotes[i], i);
但你刚宣布该函数没有返回任何内容。
它也与以后的实际定义不符:
double calculatePercentage (int totalVotes, int candidateVotes, int rosterNumber)
答案 1 :(得分:2)
您声明函数返回void
:
void calculatePercentage (int, int, int);
应该{{1}}以匹配后面的定义和用法。