[英]Getting a “Use of undeclared identifier” error
我要在 C++ 中使用不同的方法創建一個自定義String
class 。
我遇到的問題是在另一個文件中聲明的方法出現“使用未聲明的標識符”錯誤:
#include <iostream>
#include "String.h"
int main() {
std::cout << "Hello World!\n";
char str[] = "Today I am happy.";
cout<<strlen(str); // error
strlwr(str); //error
}
這是另一個文件String.h
:
#include <iostream>
using namespace std;
class Strings
{
public:
char *s;
Strings(char *a){
int l=0;
for(int i=0;a[i]!='\0';i++)
l++;
/*The length of string a*/
s=new char[l+1];
for(int i=0;a[i]!='\0';i++)
s[i]=a[i];
}
void strlwr(char *s1){ //method
for(int i=0;s1[i]!='\0';i++)
{
if(s1[i]>='A' && s1[i]<='Z')
s1[i]=(char)(s1[i]+32);
}
for(int i=0;s1[i]!='\0';i++)
cout<<s1[i];
}
int strlen(char *s1) //method
{
int l=0;
for(int i=0;s1[i]!='\0';i++)
{
l++;
}
return l;
}
int strcmp(char *s1,char *s2){
while(*s1 != '\0' && *s2 != '\0' && *s1 == *s2){
s1++;
s2++;
}
if(*s1 == *s2){
cout<<"They are equal";
return 0;
}
else if(*s1 >= *s2)
return 1;
else
return -1;
}
};
這與其他程序一起工作得很好。 我不知道為什么我現在有這個問題。
如果這些函數獨立於 class,您可以聲明它們static
。
static void strlwr(char *s1){ //method
static int strlen(char *s1) //method
他們像這樣使用它們:
cout<<String::strlen(str); // error
String::strlwr(str); //error
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.