g++编译错误.h文件

发布于 2024-12-24 19:15:35 字数 4045 浏览 5 评论 0原文

我正在尝试在 Linux Ubuntu 10.10 中使用 g++ 编译 .cpp 文件,当我尝试编译此代码时,

#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;                                                                     
}   

我在同一文件夹中拥有两个 .h 文件,但它一直说我的 writeVector.h文件或文件夹不存在。

这就是我的 writeVector.h 文件的样子

#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 文件

#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;                                                                    
}

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;                                                                    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半世蒼涼 2024-12-31 19:15:35

更改

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

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

#include "filename" 用于您创建的本地头文件。

#include 用于头文件 全局包含在 C++ 中,系统头文件

没有类似 <"filename"> 的语法

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">

恰似旧人归 2024-12-31 19:15:35

#include <"writeVector.h"

该代码无效。下面的任何一行都可以工作:

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

但后者是为系统标头保留的。

#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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文