C++ Error: no appropriate default constructor available


我定義了一個結構體,然后初始化它,結果編譯報錯

no appropriate default constructor available

代碼如下:

struct matrixXvect_func
{
	thrust::host_vector<float>& matrix;
	thrust::host_vector<float>& vector;
	int matrix_rownum;
	int matrix_colnum;

	__host__ __device__
	float operator()(const int& idx) const{
		float t=0;
		for(int col=0;col<matrix_colnum;++col){
			t+=matrix[idx*matrix_colnum+col]* vector[col];
		}
		return t;
	}
};

int main(int argc, char* argv[]){
    matrixXvect_func fn;
    return 0;   
}    

說我沒有構造函數?但是我這個是個結構體誒!結構體要什么構造函數?

原來我這個結構體中有對象的引用:thrust::host_vector<float>& matrix;

這個引用需要初始化,所以編譯提示我要有構造函數。

解決方案1:把引用改成指針就不需要初始化了。

解決方案2:把引用去掉,直接就是聲明一個變量,不過這么做賦值時就是拷貝賦值吧?似乎有違引用的初衷~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM