请查看以下代码
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
bool multiple(int,int);
void isMul(vector);
vector <int> numbers;
int enterNumber = 0;
int main()
{
cout << "Enter numbers (-1 to stop entering; -2 to exit)" << endl;
cin >> enterNumber;
while(true)
{
if(enterNumber==-1)
{
isMul(numbers);
break;
}
else
{
numbers.push_back(enterNumber);
cin >> enterNumber;
}
}
}
bool multiple(int number1, int number2)
{
if(number2%number1 == 0)
{
return true;
}
else
{
return false;
}
}
void isMul(vector numbers)
{
cout << "First Number" << setw(10) << "Second Number" << setw(10) << "isMultiplication" << endl;
for(size_t size=0;size<numbers.size();size+2)
{
cout << numbers[size] << setw(12) << numbers[size+1] << setw(12) << multiple(size,size+1);
}
}
运行此代码时出现以下错误
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory `/cygdrive/c/Users/yohan/Documents/NetBeansProjects/Excersice 6.1'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin-Windows/excersice_6.1.exe
make[2]: Entering directory `/cygdrive/c/Users/yohan/Documents/NetBeansProjects/Excersice 6.1'
mkdir -p build/Debug/Cygwin-Windows
rm -f build/Debug/Cygwin-Windows/Multiple.o.d
g++ -c -g -MMD -MP -MF build/Debug/Cygwin-Windows/Multiple.o.d -o build/Debug/Cygwin-Windows/Multiple.o Multiple.cpp
Multiple.cpp:8: error: variable or field `isMul' declared void
Multiple.cpp:8: error: missing template arguments before ')' token
Multiple.cpp: In function `int main()':
Multiple.cpp:26: error: `isMul' cannot be used as a function
Multiple.cpp: At global scope:
Multiple.cpp:51: error: variable or field `isMul' declared void
Multiple.cpp:51: error: redefinition of `int isMul'
Multiple.cpp:8: error: `int isMul' previously defined here
Multiple.cpp:51: error: missing template arguments before "numbers"
Multiple.cpp:52: error: expected `,' or `;' before '{' token
make[2]: *** [build/Debug/Cygwin-Windows/Multiple.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
nbproject/Makefile-Debug.mk:71: recipe for target `build/Debug/Cygwin-Windows/Multiple.o' failed
make[2]: Leaving directory `/cygdrive/c/Users/yohan/Documents/NetBeansProjects/Excersice 6.1'
nbproject/Makefile-Debug.mk:59: recipe for target `.build-conf' failed
make[1]: Leaving directory `/cygdrive/c/Users/yohan/Documents/NetBeansProjects/Excersice 6.1'
nbproject/Makefile-impl.mk:39: recipe for target `.build-impl' failed
BUILD FAILED (exit value 2, total time: 1s)
这是为什么?无论如何,我是C ++的新手。请帮忙!
答案 0 :(得分:3)
Multiple.cpp:8: error: variable or field `isMul' declared void
Multiple.cpp:8: error: missing template arguments before ')' token
std::vector
是模板,不是完整类型;你必须专门化isMul
的参数类型:
void isMul(vector<int>);
你应该做的真正的事情,就是抓住任何一本书,在书的“基本”部分讨论模板,阅读它然后 - 然后 - 继续编程。
答案 1 :(得分:2)
问题是你没有在矢量中给出一个类型:
void isMul(vector);
如果你想要像你需要的特定类型:
void isMul(vector<int>);
如果你想模板化,你应该这样做:
template<class T> void isMul(vector<T>);
答案 2 :(得分:0)
您应该阅读一些示例或文档。没有类似vector
类型的东西,它是一个模板。您希望使用vector<int>
作为参数类型,而不只是vector
来表示整数向量。就像你定义矢量时一样。