【问题标题】:C++ [Error] a function-definition is not allowed here before '{' token, I`m trying to change the tables in strcpy into pointersC++ [错误] 在 \'{\' 标记之前不允许函数定义,我正在尝试将 strcpy 中的表更改为指针
【发布时间】:2023-02-13 11:18:45
【问题描述】:

所以我有这个带表的 strcpy,但我需要更改它,以便没有表,只有指针。当我尝试这样做时,出现错误(我把 $$ 放在前面)

所以原来的:

#include <iostream>
using namespace std;
        
int main() {
char *mon_strcpy(char destination[], char source[]) {
    int index = 0;
    while (source[index] != '\0') {
       destination[index] = source[index];
       index++;
    }
    destination[index] = '\0';
    return destination;
}
return 0;
}

这是我试图让它发挥作用的那个:

#include <iostream>
using namespace std;
        
int main() {
    char *mon_strcpy(char *destination, char *source) $${
        int index = 0;
        while (*source != '\0') 
        {
           *destination = *source;
           index++;
        }
        *destination = '\0';
        return destination;   
    }
return 0;
}

我无法全神贯注地寻找问题所在……TIA

【问题讨论】:

    标签: c++


    【解决方案1】:

    在 C 和 C++ 中,您必须在另一个函数之外声明定义一个函数(此处为 main())。就像是:

    char *mon_strcpy(char *destination, char *source) { ... }
    
    int main () { 
      mon_strcpy(dst, src);
    }
    

    此外,$$ 符号不允许在除 cmets 之外的 C++ 代码中使用。
    这只是解决您遇到的编译器错误。

    如果功能逻辑有问题,为什么它不起作用?您可能想要调试,然后是一个新问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      • 2011-11-17
      相关资源
      最近更新 更多