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

Hea*_*r T 0 c++ methods intellisense vector

所以,我已删除并重新输入错误引用的行,关闭并重新打开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;
}
Run Code Online (Sandbox Code Playgroud)

Som*_*ude 8

你有这个声明

void calculatePercentage (int, int, int);
Run Code Online (Sandbox Code Playgroud)

然后你做

percentage = calculatePercentage(totaledVotes, candidateVotes[i], i);
Run Code Online (Sandbox Code Playgroud)

但是你刚刚声明函数没有返回任何东西.

它也与以后的实际定义不符:

double calculatePercentage (int totalVotes, int candidateVotes, int rosterNumber)
Run Code Online (Sandbox Code Playgroud)

  • 并且您删除了声明与定义不匹配的事实.为什么?它(至少是恕我直言)也是一个重要的观点.(一个有趣的观点是,由于C++作用域规则,错误的声明在定义发生时是不可见的.一个好的编译器可能会捕获它,但结果是未定义的行为,而不是错误需要诊断.另一个原因是永远不要将函数声明放在另一个函数中.) (2认同)