智能感知:不能将 "void" 类型的值分配给类型 "double" 的实体

IntelliSense: a value of type "void" cannot be assigned to an entity of type "double"

本文关键字:类型 实体 double 分配 void 感知 不能 智能      更新时间:2023-10-16

因此,我删除并重新键入了错误所指的行,关闭并重新打开了Visual Studio,并查看了此处发布的几个错误,这些错误显示了与我的相同/相似的措辞。

我有一种感觉,这是Visual Studio的错误,因为即使在我删除了所有代码,保存并重新编译之后,它在同一行上给出了不再存在的错误。

"

智能感知:不能将类型为"void"的值分配给类型为"double"的实体"

以防万一这不是工作室的错误,我想我会询问有关我的代码中可能导致此错误的任何想法?代码中间的块引用是它引用该错误的行。

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

你有这个声明

void calculatePercentage (int, int, int);

然后你做

percentage = calculatePercentage(totaledVotes, candidateVotes[i], i);

但是您刚刚声明该函数不返回任何内容。

而且它与后面的实际定义也不匹配:

double calculatePercentage (int totalVotes, int candidateVotes, int rosterNumber)

你声明返回void的函数:

void calculatePercentage (int, int, int);

这应该double,以匹配后面的定义和用法。

相关文章: