Solve the undefined identifier "string", undefined identifier "cout", "name" encountered in C++: unknown rewrite specifier error

Table of contents

Solve the undefined identifier "string", undefined identifier "cout", and "name" encountered by C++: unknown rewrite specifier error

1. Undefined identifier "string"

2. Undefined identifier "cout"

3. "name": unknown rewrite specifier error

Summarize

1. Undefined identifier "string"

2. Undefined identifier "cout"

3. "name": unknown rewrite specifier error


Solve the undefined identifier "string", undefined identifier "cout", and "name" encountered by C++: unknown rewrite specifier error

In C++ programming, we may encounter some common errors, such as undefined identifier "string", undefined identifier "cout", and "name": unknown override specifier error. These errors are usually caused by missing header file introductions or syntax errors. In this article, we will detail the causes of these errors and provide corresponding solutions.

1. Undefined identifier "string"

When we use the type ​​string​​ in C++ code, if an error of undefined identifier "string" occurs, it is usually because we forgot to introduce ​ ​<string>​Header file. The type in the standard C++ library is defined in the header file, so we need to add the following statement to the code:​string​​<string>​

cppCopy code#include <string>

This can solve the problem of undefined identifier "string". Please make sure you include the correct header files before using the ​​string​​type.

2. Undefined identifier "cout"

In C++, ​​cout​​ is the standard output stream object, used to output text information to the console. If we encounter the error of undefined identifier "cout", it is usually because we forgot to include the ​<iostream>​​ header file. ​​cout​​ is defined in the ​​<iostream>​​ header file, so we need to add the following statement to the code:

cppCopy code#include <iostream>

This can solve the problem of undefined identifier "cout". Please make sure you include the correct header files before using ​​cout​​.

3. "name": unknown rewrite specifier error

When we use inheritance in a C++ class, if a "name": unknown override specifier error occurs, it is usually because we did not correctly override the member function of the base class. In C++, if we want to redefine a member function of a base class in a derived class, we need to use the ​​override​​ keyword to make an explicit declaration. For example:

cppCopy codeclass Base {
public:
    virtual void foo();
};
class Derived : public Base {
public:
    void foo() override;  // 重新定义基类的成员函数
};

In this example, the ​​Derived​​ class redefines the ​ member of the ​​Base​​ class function and declared using the ​keyword. This will resolve the 'name': unknown rewrite specifier error. Please ensure that the member functions of the base class are correctly overridden in the derived class and declared using the ​​ keyword. ​foo​​override​​override​

Summarize

In this article, we address three common errors in C++: undefined identifier 'string', undefined identifier 'cout', and 'name': unknown override specifier error . For undefined identifier errors, we need to introduce the correct header file; for unknown override specifier errors, we need to correctly override the member functions of the base class in the derived class and use​​override​​ Keyword declaration. By understanding the causes of these errors and taking appropriate solutions, we can more effectively debug and fix problems in our C++ code. I hope this article will be helpful in solving these errors encountered in C++ programming!

We can demonstrate how to resolve these errors with the following sample code.

1. Undefined identifier "string"

cppCopy code#include <iostream>
#include <string>
int main() {
    std::string message = "Hello, World!";
    std::cout << message << std::endl;
    return 0;
}

In this sample code, we introduced the ​​<iostream>​​ and ​​<string>​​ header files and used ​​std::string​​The type defines a string variable​​message​​. Then, we use ​​std::cout​​ and ​​std::endl​​ to output the value of the variable.

2. Undefined identifier "cout"

cppCopy code#include <iostream>
int main() {
    int number = 10;
    std::cout << "The number is: " << number << std::endl;
    return 0;
}

In this sample code, we only introduced the ​​<iostream>​​ header file and used ​​std::cout​​ to output an integer variable​ The value of a>​number​​.

3. "name": unknown rewrite specifier error

cppCopy code#include <iostream>
class Base {
public:
    virtual void foo() {
        std::cout << "Base::foo()" << std::endl;
    }
};
class Derived : public Base {
public:
    void foo() override {
        std::cout << "Derived::foo()" << std::endl;
    }
};
int main() {
    Base* obj = new Derived();
    obj->foo();
    delete obj;
    return 0;
}

In this example code, we define a base class​​Base​​ and a derived class​​Derived​​. There is a virtual function in the base class. The function is redefined in the derived class and declared using the keyword . In the ​ function, we create an object pointer of the ​ class and call it through the base class pointer Function. Due to the use of the virtual function mechanism, the functions in the derived class are actually called. Through the above sample code, we can see how to correctly introduce the header file to solve the undefined identifier error, and use the ​​ keyword for correct member function rewriting. These methods can help us better handle and debug problems in C++ code. ​foo​​override​​main​​Derived​​foo​​override​

C++ is a statically typed language that provides a variety of built-in data types, including basic data types and composite data types. Below I will introduce the common data types in C++ in detail and give some sample codes for definitions.

  1. Basic data types:
  • Integer type: ​int​​, ​short​​, ​long​​, < a i=4> etc. ​long long​
cppCopy codeint num = 10;
short age = 20;
long population = 1000000;
long long bigNum = 1000000000000;
  • Floating point type:​​float​​, ​​double​​, ​​long double​​, etc.
cppCopy codefloat pi = 3.14;
double gravity = 9.8;
long double height = 1000000000.123456789;
  • Character type:​​char​​.
cppCopy codechar grade = 'A';
  • cloth type:​​bool​​.
cppCopy codebool isTrue = true;
  1. Composite data types:
  • Array type: Use square brackets​​[]​​ to define. The element type can be any basic data type or composite data type.
cppCopy codeint numbers[5] = {1, 2, 3, 4, 5};
char name[20] = "John Smith";
  • String type: defined using the ​​std::string​​ class.
cppCopy code#include <string>
std::string message = "Hello, World!";
  • Structure type: defined using the ​​struct​​ keyword, it can contain multiple member variables of different types.
cppCopy codestruct Person {
    std::string name;
    int age;
    char gender;
};
Person person1 = {"John", 25, 'M'};
  • Enumeration type: defined using the ​​enum​​ keyword, representing a set of named constants.
cppCopy codeenum Weekday {
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday
};
Weekday day = Wednesday;

These are commonly used data types in C++, and proper type selection can better suit the needs of the problem. Defining and using these data types can help us store, calculate and process data, making programs more flexible and functional.

Guess you like

Origin blog.csdn.net/q7w8e9r4/article/details/133814291