2016-11-08 4 views
0

関数を使って行列を読み込み、行列をmainに戻したいと思っています。関数から多次元ベクトル(行列)を返す方法

ここに私のコードです:

main.cppに

#include <iostream> 
#include <windows.h> 
#include <vector> 
#include "mymath.h" 
using namespace std; 

int main(){ 

vector<vector<double>> matrix_read(); 

Sleep(60000); 
return 0; 
} 

mymath.h

#pragma once 
#ifndef MYMATH_H 
#define MYMATH_H 
vector<vector<double>> matrix_read(); 
#endif 

mymath.cpp

参照用

Return multidimensional vector from function for use in main, how to use correctly?

Error list

私のエラーは何ですか?

エラーリストには重大な間違いがあることを示しています。ご協力ありがとうございました。控えめで、初心者はここにいてください。

+0

'>>は' C++の演算子は、変数と関数定義の空白。 –

+1

'mymath.h'ヘッダファイルで、' vector'とは何ですか?それについて少し考えてみてください。 –

答えて

0
  1. あなたがincludeディレクティブの前にusing namespace std;を持っていなかった場合は、ヘッダーにstd::vectorvectorの前に付ける必要があります。とにかく、ヘッダーにstd::があることをお勧めします。主に

  2. 、それは

    int main(){ 
    
        vector<vector<double>> matrix = matrix_read(); 
    
        Sleep(60000); 
        return 0; 
    } 
    

すなわちなければならないあなたは、関数の戻り値にオブジェクトmatrixを設定。それ以外の場合は、メイン関数のmatrix_readの別のプロトタイプを定義します。

+0

ありがとうございました。これはうまくいった。私は今の作業コードを答えとして掲示します。 –

0

マクシミリアンマテーが正しかった。ここでは作業コードだ:

mymath.h

#pragma once 
#include <vector> 
#include <iostream> 

std::vector<std::vector<double>> matrix_read(); 

mymath.cpp

#include "mymath.h" 


std::vector<std::vector<double>> matrix_read() { 

std::cout << "How big is the quadratic matrix A?\n"; 
int n; 
//row&column size A 
std::cin >> n; 
    std::vector<std::vector<double>> A(n, std::vector<double>(n)); 

     //fill matrix A 
     int j = 0; 
     for (int i = 0; i < n; i++) { 

      for (int j = 0; j < n; j++) { 
       std::cout << "Please enter the value for A" << "[" << i + 1 << "]" << "[" << j + 1 << "]\n"; 
       std::cin >> A[i][j]; 
      } 
     } 
     //control matrix A: 
     std::cout << "Please be sure this is the correct Matrix A: \n\n"; 
     for (int i = 0; i < n; i++) { 
     for (int j = 0; j < n; j++) { 
      std::cout << A[i][j] << " "; 
      } 
     std::cout << std::endl; 
     } 
return A; 
} 

main.cppに

#include <windows.h> 

    #include "mymath.h" 

    int main() { 

     std::vector<std::vector<double>> A = matrix_read(); 

    Sleep(60000); 
    return 0; 
    } 
関連する問題