What could be the reason for this behavior? The SparseMatrix class does not exist, there is only a SparseMatrix structure. I use Qt Creator 4.9.0, CMake 3.10.2, C ++, CUDA, Ubuntu 18.04.

sparsematrix.h

#pragma once #include <iostream> #include <unordered_map> #include <string> #include <regex> #include <fstream> #include <vector> typedef std::unordered_map<size_t,double> IndexValue; typedef std::unordered_map<std::size_t,IndexValue> Matrix; struct SparseMatrix { SparseMatrix(const std::string &A,const std::string &B) { std::ifstream in(A); std::string line; std::getline(in,line);//считали шапку in>>m_nrows; m_ncols = m_nrows+1; std::getline(in,line);//считали значение std::getline(in,line);//считали шапку-2 while(in) { std::size_t i,j; double val; in>>i>>j>>val; m_matrix[i][j] = val; } in.close(); in.open(B); std::getline(in,line); std::getline(in,line); std::getline(in,line); while(in) { std::size_t i; double val; in>>i>>val; m_matrix[i][m_nrows] = val; } std::cout<<m_matrix.size()<<'\n'; } SparseMatrix(const Matrix &matrix,std::size_t s1,std::size_t s2) { m_matrix = matrix; m_nrows = s1; m_ncols = s2; } void print() { for(std::size_t i = 0; i<m_nrows; i++) { for(std::size_t j = 0; j<m_ncols; j++) std::cout<<m_matrix[i][j]<<' '; std::cout<<'\n'; } } double get(std::size_t i,std::size_t j) { return m_matrix[i][j]; } std::vector<double> get_column(std::size_t j) { std::vector<double> R(m_nrows); for(std::size_t i = 0; i<m_nrows; i++) { R[i] = m_matrix[i][j]; } return R; } void set(double val,std::size_t i,std::size_t j) { m_matrix[i][j] = val; } std::size_t m_nrows; std::size_t m_ncols; private: Matrix m_matrix; }; 

particle.cu (30): - 1: error: error: no default constructor exists for class "SparseMatrix"

  • @AnT then what is a struct SparseMatrix? - shaman888
  • 2
    This is the SparseMatrix class. The fact that this class is declared via the struct keyword does not in any way negate the fact that it is a class. Welcome to C ++. - AnT
  • 2
    Somewhere (you did not bring the code) you have a SparseMatrix object created (trying to create :)) without passing parameters to it. And since you have other constructors, the compiler does not generate the default constructor on its own, but as such it asks you for it ... - Harry

1 answer 1

 struct SparseMatrix { SparseMatrix(){}; // добавлено SparseMatrix(const std::string &A,const std::string &B) ... 
  • And it is necessary: SparseMatrix() = default; - AnT
  • @AnT result is the same. Wikipedia describes both options, than this one is better? ru.wikipedia.org/wiki/Constructor_in default_- shaman888
  • In fact, you still have uninitialized fields m_nrows m_ncols - VTT
  • one
    So what? This is the usual plug hole. Treatment is not a disease, but a symptom. The object may well be in an inconsistent state. Therefore, I'm sorry, but I put a minus. If the answer was - add a default constructor - there would be no questions, but adding a specifically empty constructor is not a solution, if not a solution. - Harry