编译错误消息

时间:2011-09-19 10:47:08

标签: c++

  

可能重复:
  Boolean and Void problem

所以我有这个代码有一个sum函数,它对向量

中的n个元素求和

C ++语法(切换纯文本)

#include <iostream>
#include <vector> // need this in order to use vectors in the program
using namespace std;


void computeSum (vector<int> &Vec, int howMany, int total, bool success)
    //the computeSum function that will sum positive numbers in a vector
{
    int j;
    total=0;
    for(j=0;j < howMany;j++)
        if (Vec[j] > 0){
            total+=Vec[j];
        } else {
            total+=Vec[j+1];
        }
    if (howMany > Vec[j])
        success = false;
    else
        success = true;
    if (total != 0)
        success = true;
    if (success=true){
        cout << total;
    } else {
        cout << "Oops! Appears you cannot add up these numbers!";
    }

}

int main()
{
    vector<int> dataVec;
    bool success;
    int i, n, howMany, total;
    cout << "How many numbers would you like to put into the vector? \n";
    cin >> n;

    dataVec.resize(n);

    for(vector<int>::size_type i=0;i < n;i++)
    {
        cout << "Enter your number for " << i+1 << ": \n";
        cin >> dataVec[i];
    }

    cout << "How many POSITIVE numbers would you like to sum? \n";
    cin >> howMany;
    cout << "Your total is " << computeSum(dataVec, howMany, total, success);
}

在我添加

之前

C ++语法(切换纯文本)

bool success;
在main()中的

- 我唯一的错误是成功没有在main中声明 - 所以现在我声明了它,我得到了这个......

g++ -I/usr/local/include/bjarne/GUI -I -Wall -O3 -DNDEBUG -Wno-deprecated proj1.cc -o proj1
proj1.cc: In function ‘int main()’:
proj1.cc:59:76: error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits<char>]((* & std::cout), ((const char*)"Your total is ")) << computeSum((* & dataVec), howMany, total, ((int)success))’
proj1.cc:59:76: note: candidates are:
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:110:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ostream_type& (*)(std::basic_ostream<_CharT, _Traits>::__ostream_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:110:7: note: no known conversion for argument 1 from ‘void’ to ‘std::basic_ostream<char>::__ostream_type& (*)(std::basic_ostream<char>::__ostream_type&)’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:119:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__ios_type& (*)(std::basic_ostream<_CharT, _Traits>::__ios_type&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>, std::basic_ostream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:119:7: note: no known conversion for argument 1 from ‘void’ to ‘std::basic_ostream<char>::__ios_type& (*)(std::basic_ostream<char>::__ios_type&)’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:129:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:129:7: note: no known conversion for argument 1 from ‘void’ to ‘std::ios_base& (*)(std::ios_base&)’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:167:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:167:7: note: no known conversion for argument 1 from ‘void’ to ‘long int’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:171:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:171:7: note: no known conversion for argument 1 from ‘void’ to ‘long unsigned int’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:175:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(bool) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:175:7: note: no known conversion for argument 1 from ‘void’ to ‘bool’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/ostream.tcc:93:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/ostream.tcc:93:5: note: no known conversion for argument 1 from ‘void’ to ‘short int’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:182:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(short unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:182:7: note: no known conversion for argument 1 from ‘void’ to ‘short unsigned int’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/ostream.tcc:107:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/ostream.tcc:107:5: note: no known conversion for argument 1 from ‘void’ to ‘int’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:193:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:193:7: note: no known conversion for argument 1 from ‘void’ to ‘unsigned int’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:202:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:202:7: note: no known conversion for argument 1 from ‘void’ to ‘long long int’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:206:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long long unsigned int) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:206:7: note: no known conversion for argument 1 from ‘void’ to ‘long long unsigned int’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:211:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:211:7: note: no known conversion for argument 1 from ‘void’ to ‘double’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:215:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:215:7: note: no known conversion for argument 1 from ‘void’ to ‘float’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:223:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:223:7: note: no known conversion for argument 1 from ‘void’ to ‘long double’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:227:7: note: std::basic_ostream<_CharT, _Traits>::__ostream_type& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__ostream_type = std::basic_ostream<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:227:7: note: no known conversion for argument 1 from ‘void’ to ‘const void*’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/ostream.tcc:121:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>::__streambuf_type*) [with _CharT = char, _Traits = std::char_traits<char>, std::basic_ostream<_CharT, _Traits>::__streambuf_type = std::basic_streambuf<char>]
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/ostream.tcc:121:5: note: no known conversion for argument 1 from ‘void’ to ‘std::basic_ostream<char>::__streambuf_type*’
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/basic_string.h:2694:59: note: template<class _CharT, class _Traits, class _Alloc> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:451:65: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, _CharT)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:456:63: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, char)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:462:61: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, char)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:468:68: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, signed char)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:473:70: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, unsigned char)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:493:72: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const _CharT*)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/bits/ostream.tcc:323:70: note: template<class _CharT, class _Traits> std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&, const char*)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:510:5: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const char*)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:523:75: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const signed char*)
/usr/lib/gcc/i686-redhat-linux/4.6.0/../../../../include/c++/4.6.0/ostream:528:77: note: template<class _Traits> std::basic_ostream<char, _Traits>& std::operator<<(std::basic_ostream<char, _Traits>&, const unsigned char*)
make: *** [proj1] Error 1

....什么?

**我甚至在int main的末尾添加了一个返回((返回0;))并且仍然得到了错误

2 个答案:

答案 0 :(得分:4)

您正在尝试cout computeSum的返回值void。你不能cout << void

/* computeSum returns void! */
cout << "Your total is " << computeSum(dataVec, howMany, total, success);

答案 1 :(得分:1)

事实证明,该计划存在很多问题。我试图掩盖一切,但我可​​能错过了一些。而且因为我喋喋不休地说它有点长。


给出错误消息的问题与computeSum()的返回类型有关。现在,computeSum()的返回类型是void(即它不会返回任何内容),但是在发生错误的行中,您正在尝试打印computeSum()返回的任何内容。

在我看来,您希望computeSum()返回“total”,而不是作为参数给出。要做到这一点,而不是使'total'参数在computeSum()的开头声明它,并在函数的最后添加行

return total;

因此computeSum()应如下所示:

int computeSum(vector<int> &Vec, int howMany, bool success)
{
    int total = 0;

    //...

    return total;
}

添加部分也存在逻辑错误 - 例如,当一行中有两个负数时会发生什么。

这是一个更好的版本:

int total = 0;
int i = 0;            //the index of the current item being looked at
int numAddedUp = 0;   //the number of positive numbers seen

//loop while need to add up more numbers AND i is within the bounds of Vect
//so stop upon reaching end of Vect even if not added up 'howMany' items yet.
while (numAdded < howMany && i < Vect.size())
{
    if (Vect[i] > 0)
    {
        //if it's positive, add it 
        total += Vect[i];
        numAddedUp++;
    }
    i++;
}

“成功”的逻辑对我来说似乎很脆弱。如果你使用我建议的while循环,你可以改用:

if (numAddedUp == howMany)
{
    success = true;
}
else
{
    success = false;
}

或更简单地说,

success = (numAddedUp == howMany);

您可以扩展检查以检测特殊情况,例如当HowMany小于1时。以下行检查howMany是否有效以及添加的项目数与howMany匹配:

success = (howMany >= 1) && (numAddedUp == howMany);

还要留意

if (success=true){

将值true分配(=)为'成功',但您要做的是检查相等性(==)


最后,考虑一下打印输出的方式。现在main()和computeSum()都是打印的东西。理想情况下,computeSum()不会打印任何内容,总数将与bool一起返回,表示成功/失败。然而,一个更容易(但不是很好的练习)的方法是使computeSum()打印输出,甚至不打扰返回任何东西。 main()方法只需要像这样调用函数:

computeSum(dataVec, howMany);

因为输出将在computeSum()中打印,同时还有漂亮,详细,用户友好的错误消息,例如“程序无效。与它一起使用。”“你打破了程序!“