generate_random_matrix
이라는 함수를 반환 코드 으로 작성했습니다. main
함수 앞에 함수를 넣으면 코드가 제대로 작동합니다. 나는 상단에 프로토 타입을 main
기능 아래의 기능을 넣어 포함하지만, 코드는 여기에 나에게 보통의 오류C++ : 프로토 타입의`auto` 함수 반환 유형을 선언해도 공제 오류가 발생하기 전에`auto`가 사용됩니다.
error: use of ‘auto generate_random_matrix(double, double) [with int rows = 10; int cols = 10]’ before deduction of ‘auto’
auto test_matrix = generate_random_matrix<10,10>(0, 2);
를 제공 작동하지 않는 실제 코드이다. 이것이 작동하지 않는 이유에 대한 제안?
#include <vector>
#include <array>
#include <random>
#include <iostream>
std::random_device rd;
std::mt19937 gen(rd());
template <int rows, int cols>
auto generate_random_matrix(double lower_, double upper_);
int main()
{
auto test_matrix = generate_random_matrix<10,10>(0, 2);
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 10; ++j)
{
std::cout << test_matrix[i][j] << " ";
}
std::cout << "\n";
}
}
template <int rows, int cols>
auto generate_random_matrix(double lower_, double upper_)
{
std::vector<std::vector<double>> result;
std::vector<double> inner_result;
for (int i = 0; i < rows; i++) {
inner_result.erase(inner_result.begin(), inner_result.end());
for (int j = 0; j < cols; j++) {
inner_result.push_back(std::uniform_real_distribution<double>(lower_, upper_)(gen));
}
result.push_back(inner_result);
}
return result;
}
를 참조, (구현) 아직? –