2011-10-03 113 views
8

使用stl_vector.h在使用G ++編譯我得到這個錯誤。我在Linux上。克++預期「(」令牌

{ 
    if (max_size() - size() < __n) 
    __throw_length_error(__N(__s)); 

    const size_type __len = size() + std::max(size(), __n); //THE ERROR IS ON THIS LINE! 
    return (__len < size() || __len > max_size()) ? max_size() : __len; 
} 

usr/include/c++/4.5/bits/stl_vector.h:1143:40: error: expected unqualified-id before ‘(’ token

我不知道之前不合格-ID爲什麼我「M收到這個錯誤,我尋覓了一番,發現一些‘相似’的問題,但我解決不了我的

編輯:所以這裏的錯誤日誌:

In file included from /usr/include/c++/4.5/vector:65:0, 
      from ../../RL_Toolbox/include/caction.h:34, 
      from ../../RL_Toolbox/include/cagent.h:35, 
      from shortestpathQLearning.cpp:42: 
/usr/include/c++/4.5/bits/stl_vector.h:1143:40: error: expected unqualified-id before ‘(’ token 

您可以在以前的錯誤日誌「矢量」得到由標題「caction.h」像這樣叫看:

//THESE ARE THE INCLUDES IN "caction.h" 
#ifndef CACTION_H 
#define CACTION_H 
#include <stdio.h> 
#include <vector> //HERE IT CALLS <vector> 
#include <list> 
#include <map> 
#include "cbaseobjects.h" 

然後調用矢量位/ stl_vector.h這樣的:

#ifndef _GLIBCXX_VECTOR 
#define _GLIBCXX_VECTOR 1 

#pragma GCC system_header 

#include <bits/stl_algobase.h> 
#include <bits/allocator.h> 
#include <bits/stl_construct.h> 
#include <bits/stl_uninitialized.h> 
#include <bits/stl_vector.h>//HERE IT CALLS stl_vector.h 
#include <bits/stl_bvector.h> //Im actually getting the exact same error from stl_vector.h on this header 

只是從矢量(stl_vector和stl_bvector)的最後2個標題給我完全相同的錯誤,其餘都是好的。有任何想法嗎?

在此先感謝您的幫助。

+9

你碰巧有個#define max或類似的地方嗎? – user786653

+0

只是猜測,但嘗試刪除常量。 – Dabbler

+5

你是直接包含'stl_vector.h'還是包含'vector'頭文件 –

回答

1

切勿使用以雙下劃線或下劃線後跟大寫字母開頭的標識符,除非它們是由實現提供的。

編譯器和/或標準庫被允許使用 __N__s__len,等等,在他們喜歡的任何方式。

這不是很明顯,那是你的問題,但看看如果你改變所有這些標識符會發生什麼。

編輯:我錯了,你發佈的代碼是實現的一部分,所以它使用保留標識符是完全合適的。

/usr/include/c++/4.5/bits/stl_vector.h在我的系統上包含相同的代碼。您自己的代碼中很可能會導致錯誤。例如,如果我做

#include <bits/stl_vector.h> 

我得到156錯誤。正確的方法是

#include <vector> 

但如果#include <vector>之前,你一定#define宏它可能引起問題的,你看到。

向我們展示您的代碼,最好縮小到顯示錯誤的最小源文件。

+3

他顯示的代碼與他的編譯器的標準庫一起提供,所以他不能更改它,也不應該。 – ildjarn

+0

@ildjarn:很對,謝謝。我應該注意到我自己。 –

+0

@KeithThompson我加了一個更詳細的解釋。謝謝你的幫助 – user977480

2

user977480,考慮到你說「我在使用stl_vector.h時出現這個錯誤」,我假設這意味着你的代碼正在使用諸如#include <bits/stl_vector.h>#include <c++/4.5/bits/stl_vector.h>之類的東西。

#include聲明是您的問題的原因。您需要改用#include <vector>。 stl_vector.h是一個實現細節,它本身不起作用。vector頭文件在包含一些其他實現細節文件(包括定義std::max的文件)後包含位/ stl_vector.h。

+0

嘿,我在主帖中添加了一些關於這個錯誤的更多解釋,即時通訊使用#include ,但是當向量調用這個頭文件這就是當我得到編譯器錯誤時。 – user977480

9

這可能是由預處理器損壞您的代碼造成的,可能是因爲您定義了宏max。這可能發生在C庫上,因爲通常C標準允許C標準庫函數實際上是宏(儘管我只在MSVC上看到過這樣的不幸)。

要檢查,你可以

  • 預處理源與gcc -E和搜索輸出相應的代碼。檢查它是否完好無損。
  • #include <vector>之前加一條#undef max行,看看是否有幫助。
+3

謝謝,#undef max修復了這個問題:) – user977480

+1

這節省了我,我使用別人的代碼,不知道發生了什麼。 – diedthreetimes

+0

這解決了我的問題。在linux g ++中得到相同的錯誤。這是由於我的代碼中定義了宏「max」。重命名此宏後,此錯誤消失。 – rashok