2013-03-25 21 views
-1

要麼我是盲人,要麼沒有錯誤。我認爲這可能是第一選擇。請幫我找一根大海撈針。這是我的錯誤列表的一部分:錯誤:期望'='令牌和其他許多人的主要表達式

server.cpp: In function ‘int main(int, char**)’: 
server.cpp:64:16: error: expected primary-expression before ‘=’ token 
server.cpp:71:14: error: expected primary-expression before ‘=’ token 
server.cpp:71:24: error: expected primary-expression before ‘)’ token 
server.cpp:71:24: error: expected ‘;’ before ‘)’ token 
server.cpp:72:12: error: expected primary-expression before ‘=’ token 
server.cpp:80:10: error: expected primary-expression before ‘=’ token 
make: *** [server] Error 1 

,這是我的代碼的一部分:

#include <sys/types.h> 
#include <sys/socket.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <netdb.h> 
#include <iostream> 
#include <regex.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <string.h> 
#include <string> 
#include <stdlib.h> 
#include <locale.h> 
#include <cstring> 
#include <signal.h> 
#include <dirent.h> 

using namespace std; 

/* global variables */ 
// error codes 
#define ERR_OK = 0; 
#define ERR_PARAMS = 1; 
#define ERR_SOCKET = 2; 
#define ERR_BIND = 3; 
#define ERR_OTHER = 99; 

// others 
#define LISTEN_BACKLOG 50 

/* function prototypes */ 
void printErr(int EC); 
int second(int port); 

int main(int argc, char **argv) 
{ 
    int pflag = 0; 
    string pvalue; 
    int port; 
    int c; 

    opterr = 0; 
    while((c = getopt (argc, argv, "p:")) != -1) { 
    switch(c) { 
     case 'p': 
     pflag = 1; 
     pvalue.assign(optarg); 
     break; 
     case '?': 
     if(optopt == 'c') 
      fprintf(stderr, "Option -%c requires an argument.\n", optopt); 
     else if(isprint (optopt)) 
      fprintf(stderr, "Unknown option `-%c'.\n", optopt); 
     else 
      fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt); 
     return ERR_PARAMS; 
     default: 
     abort(); 
    } 
    } 

    if(pflag == 0) { 
    printErr(ERR_PARAMS); 
    return ERR_PARAMS; 
    } 
    printf ("pvalue = %s\n", pvalue.c_str()); 

    port = atoi(pvalue.c_str()); 

    second(port); 

    return ERR_OK; 
} 

我必須在整個代碼更多類似的錯誤,所以我覺得有東西就像缺少一些東西。你看到了嗎?我不。

回答

4
#define ERR_OK = 0; 
#define ERR_PARAMS = 1; 
#define ERR_SOCKET = 2; 
#define ERR_BIND = 3; 
#define ERR_OTHER = 99; 

應該是

#define ERR_OK 0 
#define ERR_PARAMS 1 
#define ERR_SOCKET 2 
#define ERR_BIND 3 
#define ERR_OTHER 99 

宏是不是一個變量。它不具有價值。這是一個預處理功能。

您出現句法錯誤的原因是因爲ERR_OK等被替換爲= 0;而不是0

+0

是的!非常感謝!我知道這將是愚蠢的,但一個小時我找不到它。 :) – 2013-03-25 23:20:10

+0

我想我現在應該抹掉這個話題,這對他人來說不是很有幫助的問題。它是否可行? – 2013-03-25 23:21:33

+1

這完全取決於你。但是,保留主題通常會更好,以便我們可以將未來的主題標記爲重複項並鏈接到此處。事實上,這可能是它自己的重複,因爲我懷疑沒有人問過這樣的問題。 – 2013-03-25 23:22:18

4

#define s爲不正確,就應該是這樣的,例如:

#define ERR_PARAMS 1 

也就是說,它們應該沒有=,而不是用分號結束。 #define是一個預處理指令。他們不遵循與C++其餘部分相同的語法規則。特別是,預處理程序指令由新行終止,而不是由分號結束。

#define ERR_PARAMS = 1; 

這實際上是定義ERR_PARAMS= 1;。如果您在代碼中將ERR_PARAMS替換爲= 1;,您將看到爲什麼會彈出一些錯誤。例如,考慮這一行:

printErr(ERR_PARAMS); 

如果更換ERR_PARAMS在這裏,你會得到:

printErr(= 1;); 

嗯,這肯定是不對的!

1
#define ERR_OK = 0; 
#define ERR_PARAMS = 1; 
#define ERR_SOCKET = 2; 
#define ERR_BIND = 3; 
#define ERR_OTHER = 99; 

應該

#define ERR_OK 0 
#define ERR_PARAMS 1 
#define ERR_SOCKET 2 
#define ERR_BIND 3 
#define ERR_OTHER 99 
5

其他的答案是正確的,問題是符號常量

#define ERR_OK = 0; 
#define ERR_PARAMS = 1; 
#define ERR_SOCKET = 2; 
#define ERR_BIND = 3; 
#define ERR_OTHER = 99; 

然而,在C++中有更好的方法來解決這些問題:

const int ERR_OK = 0; 
const int ERR_PARAMS = 1; 
const int ERR_SOCKET = 2; 
const int ERR_BIND = 3; 
const int ERR_OTHER = 99; 

或者,C和C++ b另外允許

enum ERROR_CODES { 
    ERR_OK, 
    ERR_PARAMS, 
    ERR_SOCKET, 
    ERR_BIND, 
    ERR_OTHER = 99 
}; 
+0

+1我喜歡最後的做法。這很漂亮 ;_; – 2013-03-25 23:21:44

相關問題