-2

请看下面的代码

#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++ 新手。请帮忙!

4

3 回答 3

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>);

你应该做的真正的事情是在书中的“基本”部分中获取任何讨论模板的书,阅读它,然后 - 并且只有这样 - 继续编程。

于 2012-10-07T19:13:30.213 回答
2

问题是你没有给出向量的类型:

void isMul(vector);

如果你想要一个特定的类型,比如 ints,你需要:

void isMul(vector<int>);

如果您想对其进行模板化,您应该这样做:

template<class T> void isMul(vector<T>);
于 2012-10-07T19:13:45.547 回答
0

您可能应该阅读一些示例或文档。没有像vector类型这样的东西,它是一个模板。您想vector<int>用作参数类型,而不仅仅是vector表示整数向量。就像定义向量时所做的一样。

于 2012-10-07T19:13:24.000 回答