You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++初学者:char*初始化自定义类对象及编译报错问题

Troubleshooting Your C++ Class Initialization & Loop Issues

Hey there! Let's work through your C++ problems one by one—since you're just starting out, I'll keep this clear and practical.

1. Initializing T a = s with a char*

Right now, your custom class T doesn't know how to handle a char* input. To fix this, you need to add a constructor that accepts a const char* (using const is safer because string literals like "abcde" are read-only). Here's how to update your class:

#include <cstring> // For std::strlen, if you use C-style strings

class T {
private:
    const char* m_str; // Stores the input string
    size_t m_length;   // Stores the string length

public:
    // Constructor that takes a const char*
    T(const char* s) : m_str(s), m_length(std::strlen(s)) {}
};

If you want to avoid manual memory management (a common pitfall for beginners), using std::string instead of a raw char* is even better:

#include <string>

class T {
private:
    std::string m_str;

public:
    T(const char* s) : m_str(s) {} // std::string handles length automatically!
};

With either setup, T a = s; will work because the compiler knows to use your new constructor to create the T object from the char*.

2. Making a() Return the String Length

The line for(int i = 0; i < a(); i++) is failing because your T class doesn't have a member function (or operator) that lets you call a() like that. You have two good options here:

Option 1: Overload the () operator

This lets you keep the a() syntax you wanted. Add this to the public section of your T class:

size_t operator()() const {
    // If using C-style strings:
    return m_length;
    // If using std::string:
    // return m_str.size();
}

Now a() will return the length of your string, and your loop will compile.

Option 2: Use a named member function (more idiomatic C++)

Most C++ developers prefer clear names over operator overloading for readability. Add a function like size() or length() instead:

size_t length() const {
    return m_length; // or m_str.size() for std::string
}

Then update your loop to:

for(int i = 0; i < a.length(); i++) {
    // Your loop logic here
}

3. Fixing the invalid initialization of non-const reference... Error

This error pops up when you try to bind a temporary T object (an rvalue, like T("abcde")) to a non-const reference (T&). For example, if you have a function like this:

void someFunction(T& obj) { /* ... */ }

And you call it like:

someFunction(T("abcde"));

The temporary T object can't be passed to a non-const reference. The fix is to change the function parameter to a const reference, which accepts temporary objects:

void someFunction(const T& obj) { /* ... */ }

Check your code for any functions that take T& but don't modify the object—switching to const T& will resolve this error.


内容的提问来源于stack exchange,提问作者Khalil Kasmi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:40:18