2013-07-12 73 views
0

不工作這是我在.h文件中的函數:返回字符串;而不大括號

static std::string ReturnString(std::string some_string) 
    return ("\t<" + some_string + " "); 

編譯器(克++ -std =的C++ 0x -pedantic -Wall -Wextra)拋出這些錯誤:

error:expected identifier before '(' token 
error:named return values are no longer supported 
error:expected '{' at end of input 
warning: no return statement in function returning non-void [-Wreturn-type] 

但是,

static std::string ReturnString(std::string some_string) 
{ 
    return ("\t<" + some_string + " "); 
} 

工作正常。 偶,

static std::string ReturnString(std::string some_string) 
{ 
    return "\t<" + some_string + " "; 
} 

也可以。

請問有人請給我解釋一下嗎?我是否缺少一些字符串的基本知識?

謝謝。

+6

的功能只是基礎知識:你總是要附上大括號之間的函數體,即使它只有一條線長。這就是它。 – Nbr44

回答

1
static std::string ReturnString(std::string some_string) 
    return ("\t<" + some_string + " "); 

它實際上是C++的一個基本知識,你錯過了。在C++中,函數體必須用大括號括起來,{}。因此,上述函數的正確定義是:

static std::string ReturnString(std::string some_string) 
{ 
    return ("\t<" + some_string + " "); 
} 
+0

啊!謝謝。尷尬。之前在與{}的同一行中完成了一行代碼功能,只是進入第二行使我忘記了括號。 – sunam

0

這與字符串無關。這是關於你如何定義你的功能。在這種情況下,ReturnString是一個返回字符串的函數。

通用格式C++函數的定義是:

ReturnType NameOfTheFunction(Parameters) 
{ 
    Implementation 
} 
+0

或'auto NameOfTheFunction(Parameters) - > ReturnType {Implementation}'。 –

+0

或即將'自動名稱(參數){返回/ *東西* /; };':) – jrok

+0

最後一個分號不是正確的?但這是標準做法嗎? – sunam

相關問題