c++ - IntelliSense:类型 "void"的值不能分配给类型 "double"的实体

标签 c++ methods vector intellisense

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

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

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

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

#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)

关于c++ - IntelliSense:类型 "void"的值不能分配给类型 "double"的实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21940243/

相关文章:

c++ - 设置 VisualC++ 以使用 OpenGL 4.1?

c++ - 我可以将 vector<bool> 的内容直接从内部缓冲区写入流吗?

c++ - 如何从函数返回 vector

c++ - 如何填充 'Tree' 结构 'Declaratively'

c++ - 为什么我的 C++ 移动构造函数没有被调用?

c++ - 通过模板跟踪编译器错误

android - 为什么我不能使用 getFilesDir();在静态环境中?

java - JAVA如何通过参数调用方法

javascript - 对象函数属性中的 React 类 : Referencing class as "this",

python - 将 Numpy 数组与新网格、过滤器和缩放对齐