g++ compile error .h file

∥☆過路亽.° 提交于 2019-12-12 03:05:15

问题


I'm trying to compile a .cpp file using g++ in Linux Ubuntu 10.10 and when i try to compile this code

#include <iostream>                                                             
#include <vector>                                                               
#include <"writeVector.h"                                                       
#include <"insertionSort.h">                                                    
using namespace std;  



int main()                                                                      
{                                                                               
  int n;                                                                        
  int i;                                                                        
  vector<int> V;                                                                
  cout << "Enter the amount of numbers you want to evaluate: ";                 
  cin >> n;                                                                     
  cout << "Enter your numbers to be evaluated: " << endl;                       
  while (V.size() < n && cin >> i){                                             
   V.push_back(i);                                                              
  }   

  InsertionSort(V);                                                             
  write_vector(V);                                                              
  return 0;                                                                     
}   

I have both .h files in the same folder but it keeps saying that my writeVector.h file or folder does not exist.

This is what my writeVector.h file looks like

#include <iostream>                                                             
#include <vector>                                                               
using namespace std;                                                            

template <typename T>                                                          

void write_vector(const vector<T>& V)                                          

{                                                  

  cout << "The numbers in the vector are: " << endl;                            
  for(int i=0; i < V.size(); i++)                                                                                                                             
    cout << V[i] << " ";                                                       

}       

insertionSort.h file

#include <iostream>                                                             
#include <vector>                                                               
using namespace std;                                                            

void InsertionSort(vector<int> &num)                                            
{                                                                               
     int i, j, key, numLength = num.length( );                                  
     for(j = 1; j < numLength; j++)    // Start with 1 (not 0)                  
    {                                                                           
           key = num[j];                                                        
           for(i = j - 1; (i >= 0) && (num[i] < key); i--)   // Smaller values move up                                                                         
          {                                                                     
                 num[i+1] = num[i];                                             
          }                                                                     
         num[i+1] = key;    //Put key into its proper location                  
     }                                                                          
     return;                                                                    
}

回答1:


Change

#include <"writeVector.h"                                                       
#include <"insertionSort.h">  

to

#include "writeVector.h"                                                       
#include "insertionSort.h"

#include "filename" is used for local header files, which are made by you.

#include <filename> is used for header files Globally included in C++, System header files

there is no syntax like <"filename">




回答2:


#include <"writeVector.h"

That code is not valid. Either of the below lines would work:

#include "wrtieVector.h"
#include <writeVector.h>

but the latter is reserved for system headers.



来源:https://stackoverflow.com/questions/8691204/g-compile-error-h-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!