2017-08-03 118 views
-3

我嘗試使用向量惠特指針和模塊。 我有這個問題與C++: 在main.cpp中:錯誤無效類型參數的一元'*'(有'int')與模塊

#include<iostream> 
    using namespace std; 
    #include "Funcion1.hpp" 

int main (int argc, char *argv[]) { 
    int vec[20]; 
    int *punteroV = &vec[0]; 
    for(int i = 0;i < 10;i++){ 
     cout<<"Ingrese numero: "; 
     cin>>vec[i]; 
    } 
    cout<<FUN(*punteroV) << endl; 
    return 0; 
} 

和模塊中:

#include "Funcion1.hpp" 
#include<iostream> 
using namespace std; 
int FUN(int &punteroV){ 

    int num; 
    for(int i = 0;i<10;i++){ 
     for(int j = 0;j<10;j++){ 
      cout<<"i: "<<(punteroV+ i)<<endl<<"j: "<<(punteroV + j)<<endl; 
      if(*(punteroV + i) > *(punteroV + j)){ 
       num = (punteroV + i); 
      } 
     } 
    } 
    return num; 
} 

和.HPP

#ifndef FUNCION1_H 
#define FUNCION1_H 
int FUN(int&); 
#endif 

編譯器產生錯誤的模塊中:

error invalid type argument of unary '*' (have 'int') 

這個錯誤的含義是什麼?

+0

這意味着'punterOv + i'是'int',而不是指針。 –

+0

[錯誤:一元'\ *'(有'int')]的無效類型參數可能的重複(https://stackoverflow.com/questions/33521200/error-invalid-type-argument-of-unary-have- int) – melpomene

+0

是int'* punteroV =&vec [0];' –

回答

0

在功能FUN你有這樣一行:

if(*(punteroV + i) > *(punteroV + j)) 

你正在嘗試做的到整數的參考指針運算,然後尊重它像一個int*你不能這樣做直接的參考。要做參考數學,你必須首先採取這樣的地址:

if(*(&punteroV + i) > *(&punteroV + j)){ 
相關問題