我正在修改機器學習項目的C++技能。所以,我正在研究動態分配內存功能。我的想法是將靜態函數定義爲類,然後將它們放入一個類中,然後根據需要調用它們。所以我定義如下命名Utils.h headerfile -關於C++中的動態數組構造
//Utils.h
#include <stdio.h>
class Utils
{
public:
template<class T> static T* create1DArray(int size);
};
然後創建以下Utils.cpp爲 -
//Utils.cpp
#include<stdio.h>
#include<malloc.h>
#include"Utils.h"
template<class T> T* Utils::create1DArray(int size)
{
T* a = new T [size];
return a;
}
然後,我測試了他們作爲 -
#include<iostream>
#include<conio.h>
#include"Utils.cpp"
using namespace std;
int main()
{
cout<<"Test";
double* a;
int n=3;
a = Utils.create1DArray<double>(n);
getch();
return 0;
}
但我得到以下錯誤 -
error C2275: 'Utils' : illegal use of this type as an expression
error C2228: left of '.create1DArray' must have class/struct/union
error C2062: type 'double' unexpected
但令人驚訝的是,當我將這個函數作爲一個獨立的函數使用時,它並沒有放入函數內部。任何想法如何解決它?
使用'std :: vector'。對不起,我的意思是:使用''''範圍運算符來訪問一個類的靜態成員,而不是'.'點運算符,它是爲訪問一個非靜態成員而保留的。 – 2012-03-27 02:19:32
這些技術上不是動態數組,只是動態分配的數組。動態數組將根據內容的大小而增減。 – 2012-03-27 02:20:52
thanx Luc很多,我忘了這個語法....一直工作3個小時;我現在非常需要休息......! – Neo182 2012-03-27 02:21:51