use_str.cpp

  name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5572165936844014&dt=1194442938015&lmt=1194190197&format=336x280_as&output=html&correlator=1194442937843&url=file%3A%2F%2F%2FC%3A%2FDocuments%2520and%2520Settings%2Flhh1%2F%E6%A1%8C%E9%9D%A2%2FCLanguage.htm&color_bg=FFFFFF&color_text=000000&color_link=000000&color_url=FFFFFF&color_border=FFFFFF&ad_type=text&ga_vid=583001034.1194442938&ga_sid=1194442938&ga_hid=1942779085&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="allowtransparency"> #include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <stdio.h>

class Strings {
   char *p;
   int size;
 public:
   Strings(char *str);
   Strings(void);
   Strings(const Strings &obj);           // Copy constructor
   ~Strings(void) {delete [] p;}

   friend ostream &operator<<(ostream &stream, Strings &obj);
   friend istream &operator>>(istream &stream, Strings &obj);

   Strings operator=(Strings &obj);       // assign a Strings object
   Strings operator=(char *s);            // assign a quoted string
   Strings operator+(Strings &obj);       // concatenate a Strings object
   Strings operator+(char *s);            // concatenate a quoted string
   friend Strings operator+(char *s, Strings &obj);
            /* concatenates a quoted string with a Strings object */

   Strings operator-(Strings &obj);       // subtract a Strings object
   Strings operator-(char *s);            // subtract a quoted string

 /* relational operators between Strings objects. Note that the operators could
    just as easily return bool, rather than int */

   int operator==(Strings &obj) {return !strcmp(p, obj.p);}
   int operator!=(Strings &obj) {return strcmp(p, obj.p);}
   int operator<(Strings &obj) {return strcmp(p, obj.p) < 0;}
   int operator>(Strings &obj) {return strcmp(p, obj.p) > 0;}
   int operator<=(Strings &obj) {return strcmp(p, obj.p) <= 0;}
   int operator>=(Strings &obj) {return strcmp(p, obj.p) >= 0;}

 /* relational operators between Strings object and a quoted character string.
    Note that the operators could just as easily return bool, rather than int */

   int operator==(char *s) {return !strcmp(p, s);}
   int operator!=(char *s) {return strcmp(p, s);}
   int operator<(char *s) {return strcmp(p, s) < 0;}
   int operator>(char *s) {return strcmp(p, s) > 0;}
   int operator<=(char *s) {return strcmp(p, s) <= 0;}
   int operator>=(char *s) {return strcmp(p, s) >= 0;}

   int strsize(void) {return strlen(p);}      // return string size
   void makestr(char *s) {strcpy(s, p);}  // make quoted string from Strings object

   operator char *(void) {return p;}          // conversion to char
 };

Strings::Strings(void)
 {
   size = 1;
   p = new char[size];
   if(!p)
    {
      cout << "Allocation error!" << endl;
      exit(1);
    }
   *p = '/0';
 }

Strings::Strings(char *str)
 {
   size = strlen(str) + 1;
   p = new char[size];
   if(!p)
    {
      cout << "Allocation error!" << endl;
      exit(1);
    }
   strcpy(p, str);
 }

Strings::Strings(const Strings &obj)
 {
   size = obj.size;
   p = new char[size];
   if(!p)
    {
      cout << "Allocation error!" << endl;
      exit(1);
    }
   strcpy(p, obj.p);
 }

ostream &operator<<(ostream &stream, Strings &obj)
 {
   stream << obj.p;
   return stream;
 }

istream &operator>>(istream &stream, Strings &obj)
 {
   char t[255];      // arbitrary string length--yours can be larger
   int len;

   for(len=0; len<255; len++)
    {
      stream.get(t[len]);
      if(t[len]=='/n')
         break;
      if(t[len]=='/b')
         if(len)
          {
            len--;
            cout << "'/b'";
          }
    }
   t[len]='/0';
   len++;

   if(len>obj.size)
    {
      delete obj.p;
      obj.p = new char[len];
      if(!obj.p)
       {
         cout << "Allocation error!" << endl;
         exit(1);
       }
      obj.size = len;
    }
   strcpy(obj.p, t);
   return stream;
 }

Strings Strings::operator=(Strings &obj)
  {
   Strings temp(obj.p);

   if(obj.size > size)
    {
      delete p;
      p = new char[obj.size];
      size = obj.size;
      if(!p)
       {
         cout << "Allocation error!" << endl;
         exit(1);
       }
    }
   strcpy(p, obj.p);
   strcpy(temp.p, obj.p);
   return temp;
 }

Strings Strings::operator=(char *s)
  {
   int len = strlen(s) + 1;

   if(size < len)
    {
      delete p;
      p = new char[len];
      size = len;
      if(!p)
       {
         cout << "Allocation error!" << endl;
         exit(1);
       }
    }
   strcpy(p, s);
   return *this;
 }

Strings Strings::operator+(Strings &obj)
 {
   int len;
   Strings temp;

   delete temp.p;
   len = strlen(obj.p) + strlen(p) + 1;
   temp.p = new char[len];
   temp.size = len;
   if(!temp.p)
    {
      cout << "Allocation error!" << endl;
      exit(1);
    }
   strcpy(temp.p, p);
   strcat(temp.p, obj.p);
   return temp;
 }

Strings Strings::operator+(char *s)
 {
   int len;
   Strings temp;

   delete temp.p;
   len = strlen(s) + strlen(p) + 1;
   temp.p = new char[len];
   temp.size = len;
   if(!temp.p)
    {
      cout << "Allocation error!" << endl;
      exit(1);
    }
   strcpy(temp.p, p);
   strcat(temp.p, s);
   return temp;
 }

Strings operator+(char *s, Strings &obj)
 {
   int len;
   Strings temp;

   delete temp.p;
   len = strlen(s) + strlen(obj.p) + 1;
   temp.p = new char[len];
   temp.size = len;
   if(!temp.p)
    {
      cout << "Allocation error!" << endl;
      exit(1);
    }
   strcpy(temp.p, s);
   strcat(temp.p, obj.p);
   return temp;
 }

Strings Strings::operator-(Strings &substr)
 {
   Strings temp(p);
   char *s1;
   int i,j;

   s1 = p;
   for(i=0; *s1; i++)
    {
      if(*s1!=*substr.p)
       {
         temp.p[i] = *s1;
         s1++;
       }
      else
       {
         for(j=0; substr.p[j]==s1[j] && substr.p[j]; j++)
            ;
         if(!substr.p[j])
          {
            s1 += j;
            i--;
          }
         else
          {
            temp.p[i] = *s1;
            s1++;
          }
       }
    }
    temp.p[i] = '/0';
    return temp;
 }

Strings Strings::operator-(char *substr)
 {
   Strings temp(p);
   char *s1;
   int i,j;

   s1 = p;
   for(i=0; *s1; i++)
    {
      if(*s1!=*substr)
       {
         temp.p[i] = *s1;
         s1++;
       }
      else
       {
         for(j=0; substr[j]==s1[j] && substr[j]; j++)
            ;
         if(!substr[j])
          {
            s1 += j;
            i--;
          }
         else
          {
            temp.p[i] = *s1;
            s1++;
          }
       }
    }
    temp.p[i] = '/0';
    return temp;
 }

 void main(void)
  {
   Strings s1("A sample program which uses string objects./n");
   Strings s2(s1);
   Strings s3;
   char s[80];

   cout << s1 << s2;
   s3 = s1;
   cout << s3;
   s3.makestr(s);
   cout << "Converted to a string: " << s;

   s2 = "This is a new string.";
   cout << s2 << endl;

   Strings s4("This is a new string, too.");
   s1 = s2 + s4;
   cout << s1 << endl;

   if(s2==s3)
      cout << "Strings are equal." << endl;
   if(s2!=s3)
      cout << "Strings are not equal." << endl;
   if(s1<s4)
      cout << "s1 is less than s4." << endl;
   if(s1>s4)
      cout << "s1 is greater than s4." << endl;
   if(s1<=s4)
      cout << "s1 is less than or equal to s4." << endl;
   if(s1>s4)
      cout << "s1 is greater than or equal to s4." << endl;

   if(s2 > "ABC")
      cout << "s2 is greater than 'ABC'" << endl << endl;

   s1 = "one two three one two three/n";
   s2 = "two";
   cout << "Initial string: " << s1;
   cout << "String after subtracting two: ";
   s3 = s1 - s2;
   cout << s3;

   cout << endl;
   s4 = "Jamsa's C/C++ ";
   s3 = s4 + "Programmer's Bible/n";
   cout << s3;
   s3 = s3 - "C/C++";
   s3 = "This is " + s3;
   cout << s3;

   cout << "Enter a string: ";
   cin >> s1;
   cout << s1 << endl;
   cout << "s1 is " << s1.strsize() << " characters long." << endl;
   puts(s1);

   s1 = s2 = s3;
   cout << s1 << s2 << s3;
   s1 = s2 = s3 = "Program finished./n";
   cout << s1 << s2 << s3;
 }

 

 

 

 


<think>好的,我现在需要解决用户遇到的编译错误:use of undeclared identifier 'TimeZone',出现在chrome_browser_main.cc文件中,涉及switches::kTimeZone。用户提到了引用中的内容,其中引用[1]讨论了时区缩写和Olson数据库,引用[2]关于Perl和C++的引用传递,引用[3]是编译错误的具体位置。 首先,我需要理解这个错误的根本原因。未声明的标识符通常意味着编译器在当前位置找不到对应的定义。这可能是因为头文件未包含、命名空间不正确或者符号被重命名/废弃。 用户的问题涉及Chrome的源代码中的switches::kTimeZone。switches命名空间通常包含命令行开关相关的常量,这些常量可能在src/chrome/common/chrome_switches.h中定义。如果kTimeZone未在该头文件中声明,或者用户使用的代码版本中该符号已被移除或改名,就会导致此错误。 接下来,检查引用内容是否有帮助。引用[1]提到时区标识符和Olson数据库,可能提示代码中需要正确的时区处理,但可能与编译错误无直接关联。引用[3]中的错误是关于PTRACE_GETREGS未声明,解决方法可能需要检查头文件包含,类似地,这里的问题可能也是缺少包含某个头文件。 可能的解决步骤包括: 1. 确认是否包含了定义switches::kTimeZone的头文件,通常是chrome_switches.h。 2. 检查代码的版本,确认kTimeZone是否存在,或者是否已被替换为其他名称(例如kTimeZoneOverride)。 3. 如果符号存在但命名空间不对,是否需要使用正确的命名空间。 4. 确保依赖项和代码同步,没有版本不兼容的问题。 另外,根据Chrome的代码历史,某些开关可能会被更新或废弃。例如,kTimeZone可能已被更名为kTimeZoneOverride,需要查阅最新的代码或提交记录确认。 总结,用户需要检查头文件包含、符号的存在性以及可能的命名变更,同时确保代码库是最新的。此外,参考类似问题的解决方式,如引用[3]中的错误通过包含正确的头文件解决,这里可能也是类似情况。</think>要解决编译错误`use of undeclared identifier 'TimeZone'`(涉及`switches::kTimeZone`),需逐步排查以下可能原因: --- ### 1. **检查头文件包含** - **问题**:`switches::kTimeZone`的定义通常位于Chrome源码的`chrome/common/chrome_switches.h`头文件中[^3]。 - **解决**: ```cpp #include "chrome/common/chrome_switches.h" // 确保包含此头文件 ``` - 若已包含但仍报错,需验证头文件路径是否正确,或检查编译器的包含路径设置。 --- ### 2. **确认符号是否被废弃或更名** - **问题**:Chrome的代码库会随时间更新,某些符号可能被重命名或移除。例如: - `kTimeZone`可能已被替换为`kTimeZoneOverride`(具体以代码版本为准)。 - **解决**: - 在代码库中全局搜索`kTimeZone`,确认其最新名称。 - 查阅Chromium代码提交历史,例如通过[Chromium Code Search](https://source.chromium.org/)确认符号状态。 --- ### 3. **命名空间验证** - **问题**:`switches`是Chrome中定义命令行参数的命名空间,需确保符号在此命名空间下。 - **解决**: ```cpp // 正确使用方式 std::string timezone = switches::kTimeZone; ``` - 若使用了`using namespace switches;`,需检查是否与其他命名空间冲突。 --- ### 4. **代码同步与依赖更新** - **问题**:若本地代码与仓库版本不一致,可能导致符号缺失。 - **解决**: - 执行`git pull`同步最新代码。 - 运行`gclient sync`更新依赖项。 --- ### 5. **编译环境检查** - **问题**:编译器或构建工具链配置错误可能导致头文件无法识别。 - **解决**: - 清理构建缓存:`gn clean out/<BuildDir>` - 重新生成Ninja文件:`gn gen out/<BuildDir>` --- ### 关联问题参考 - 类似错误`error: use of undeclared identifier 'PTRACE_GETREGS'`通常通过包含正确头文件(如`<sys/ptrace.h>`)解决,本例可类比排查头文件缺失问题。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值