C ++调用模板函数错误 [英] C++ Calling Template Function Error

查看:124
本文介绍了C ++调用模板函数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试编译以下代码时:

int main(){

    Array<int> *testArray = new Array<int>(5);
    testArray->initArray<int>(testArray);
    testArray->printArray<int>(testArray);



    return 0;
}

使用此模板:

template<typename T>
class Array{
public:
    Array(int size){
        size = size;
        data = new T[size];
    };
    Array<T> addData(T dataToAdd){
        Array <T> *tmp = new Array <T> (this->size);
        tmp = this->data;
        Array <T> *newData = new Array<T> (this->size + 1);

        for (int i = 0; i < this->size + 1; ++i){
            if (i < this->size){
                newData->data[i] = tmp[i];
            }
            else{
                newData->data[i] = dataToAdd;
            }
        }
        return newData;
    };
    void initArray(T arrayToInit){
        for (int i = 0; i < this->size; ++i){
            this->data[i] = i;
        }
    };
    void printArray(T arrayToPrint){
        ostringstream oss;
        string answer = "";

        for (int i = 0; i < arrayToPrint->size; ++i){
            oss << arrayToPrint[i] + " ";
        }

        answer = oss.str();

        cout << answer << endl;
    };

private:
    int size;
    T* data;
};

我在int main()中得到以下错误:

I get the following error in my int main() :

对于我的两个函数调用(initArray和printArray),'int'

"expected primary-expression before ‘int’"

我对C ++相当新,特别是对模板没有什么经验,任何建议将非常感谢。非常感谢您的时间。

for both of my function calls (initArray and printArray). I am fairly new to C++ and have little experience with templates in particular, any advice would be greatly appreciated. Thank you for your time.

编辑:感谢大家的回应和建设性的批评,我已经取得了巨大的进步,感谢大家的帮助。

Thank you everyone for the responses and constructive criticism, I have made a great amount of progress thanks to everybody's help.

推荐答案

在这种情况下,您不需要< int> 。 (考虑 std :: vector< int> ,您将写入 hoge.push_back(0) c $ c> hoge.push_back< int>(0)。)

You don't need <int> to call the function in this case. (Thinking about std::vector<int>, you will write hoge.push_back(0) instead of hoge.push_back<int>(0).)

数组:;:printArray 也是无效和不需要的。

The arguments of Array::initArray and Array:;:printArray are also invalid and unneeded.

不会发出编译错误/警告,但无法正常工作):

Fixed code (emits no compile errors/warnings, but not working properly):

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

template<typename T>
class Array{
public:
    Array(int size){
        size = size;
        data = new T[size];
    };
    Array<T> addData(T dataToAdd){
        Array <T> *tmp = new Array <T> (this->size);
        tmp = this->data;
        Array <T> *newData = new Array<T> (this->size + 1);

        for (int i = 0; i < this->size + 1; ++i){
            if (i < this->size){
                newData->data[i] = tmp[i];
            }
            else{
                newData->data[i] = dataToAdd;
            }
        }
        return newData;
    };
    void initArray(){
        for (int i = 0; i < this->size; ++i){
            this->data[i] = i;
        }
    };
    void printArray(){
        ostringstream oss;
        string answer = "";

        for (int i = 0; i < this->size; ++i){
            oss << data[i] + " ";
        }

        answer = oss.str();

        cout << answer << endl;
    };

private:
    int size;
    T* data;
};

int main(){

    Array<int> *testArray = new Array<int>(5);
    testArray->initArray();
    testArray->printArray();



    return 0;
}

这篇关于C ++调用模板函数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆