【问题标题】:IntelliSense: a value of type "void" cannot be assigned to an entity of type "double"IntelliSense:“void”类型的值不能分配给“double”类型的实体
【发布时间】:2014-03-23 07:27:27
【问题描述】:

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

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

“IntelliSense:“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;
}

【问题讨论】:

    标签: c++ methods vector intellisense


    【解决方案1】:

    你有这个声明

    void calculatePercentage (int, int, int);
    

    那你就做

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

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

    而且后面的实际定义也不符合:

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

    【讨论】:

    • 我现在觉得自己很蠢。我改变了方法,完全忘记了声明。我想念Java。谢谢!
    • 并且您删除了声明与定义不匹配的事实。为什么?这也是(至少恕我直言)重要的一点。 (同样有趣的一点是,由于 C++ 范围规则,错误的声明在定义发生的地方是不可见的。一个好的编译器无论如何都可能捕获它,但结果是未定义的行为,而不是错误需要诊断。另一个原因是永远不要将函数声明放在另一个函数中。)
    【解决方案2】:

    你声明函数返回void:

    void calculatePercentage (int, int, int);
    

    应该是double,以配合后面的定义和用法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-02
      • 1970-01-01
      • 2019-03-27
      相关资源
      最近更新 更多