gpt4 book ai didi

c++ - 由于 vector 中的值作为函数参数,程序在编译时失败

转载 作者:行者123 更新时间:2023-11-30 02:59:19 25 4
gpt4 key购买 nike

请看下面的代码

#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <sstream>


using namespace std;
bool isEven(int);


int number;
vector<int> inputNumbers;
string line;

int main()
{
cout << "Please enter numbers: ";

getline(std::cin,line);
istringstream iss(line);

while(iss>>number)
{
inputNumbers.push_back(number);
}

size_t vectorSize = inputNumbers.size();

for(int i=0;i<vectorSize;i++)
{
cout << "Result for number " << i << "is: " << isEven(inputNumbers(i));
}


}

bool isEven(int number)
{
if(number%2 == 0)
{
return true;
}
else
{
return false;
}
}

当我运行这个程序时,它给出了以下错误

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/cygdrive/c/Users/yohan/Documents/NetBeansProjects/Excersice 6'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin-Windows/excersice_6.exe
make[2]: Entering directory `/cygdrive/c/Users/yohan/Documents/NetBeansProjects/Excersice 6'
mkdir -p build/Debug/Cygwin-Windows
rm -f build/Debug/Cygwin-Windows/EvenNumbers.o.d
g++ -c -g -MMD -MP -MF build/Debug/Cygwin-Windows/EvenNumbers.o.d -o build/Debug/Cygwin-Windows/EvenNumbers.o EvenNumbers.cpp
EvenNumbers.cpp: In function `int main()':
EvenNumbers.cpp:32: error: no match for call to `(std::vector<int, std::allocator<int> >) (int&)'
make[2]: *** [build/Debug/Cygwin-Windows/EvenNumbers.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
nbproject/Makefile-Debug.mk:79: recipe for target `build/Debug/Cygwin-Windows/EvenNumbers.o' failed
make[2]: Leaving directory `/cygdrive/c/Users/yohan/Documents/NetBeansProjects/Excersice 6'
nbproject/Makefile-Debug.mk:62: recipe for target `.build-conf' failed
make[1]: Leaving directory `/cygdrive/c/Users/yohan/Documents/NetBeansProjects/Excersice 6'
nbproject/Makefile-impl.mk:39: recipe for target `.build-impl' failed


BUILD FAILED (exit value 2, total time: 1s)

如果我删除对 isEven() 的函数调用,就不会发生这种情况。为什么会这样?请帮忙!

最佳答案

isEven(inputNumbers(i)) 更改为 isEven(inputNumbers[i])

您打算访问第 ith<​​ 个输入数字,为此您使用了括号运算符。

关于c++ - 由于 vector 中的值作为函数参数,程序在编译时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12990991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com