C++中的空字符常量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31193454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:53:01  来源:igfitidea点击:

Empty character constant in c++

c++

提问by BeepBoop

I copied this code from a tutorial to play around with, however I kept on getting an error that stated that I can't have any empty character constants. the tutorial was in VS 2008, and I am using VS 2013, so perhaps this is no longer valid, but I can't find any fix. here is the code:

我从教程中复制了这段代码来玩,但是我一直收到一个错误,指出我不能有任何空字符常量。该教程是在 VS 2008 中使用的,我使用的是 VS 2013,所以这可能不再有效,但我找不到任何修复方法。这是代码:

#include "stdafx.h"
#include <iostream>

class MyString
{
private:
    char *m_pchString;
    int m_nLength;

public:
MyString(const char *pchString="")
{
    // Find the length of the string
    // Plus one character for a terminator
    m_nLength = strlen(pchString) + 1;

    // Allocate a buffer equal to this length
    m_pchString = new char[m_nLength];

    // Copy the parameter into our internal buffer
    strncpy(m_pchString, pchString, m_nLength);

    // Make sure the string is terminated
    //this is where the error occurs
    m_pchString[m_nLength-1] = '';
}

~MyString() // destructor
{
    // We need to deallocate our buffer
    delete[] m_pchString;

    // Set m_pchString to null just in case
    m_pchString = 0;
}

    char* GetString() { return m_pchString; }
    int GetLength() { return m_nLength; }
};

int main()
{
    MyString cMyName("Alex");
    std::cout << "My name is: " << cMyName.GetString() << std::endl;
    return 0;
} 

The error I get is the following:

我得到的错误如下:

Error   1   error C2137: empty character constant       

Any help will be greatly appreciated

任何帮助将不胜感激

Thanks again.

再次感谢。

回答by Tommy Andersen

This line:

这一行:

m_pchString[m_nLength-1] = '';

What you probably mean is:

你的意思大概是:

m_pchString[m_nLength-1] = '
m_pchString[m_nLength-1] = 0;
';

Or even:

甚至:

##代码##

Strings are zero terminated, which is written as a plain 0or the null character '\0'. For double quote strings ""the zero termintation character is implicitly added to the end, but since you explicitly set a single character you must specify which.

字符串以零结尾,写为普通0字符或空字符'\0'。对于双引号字符串"",零终止字符被隐式添加到末尾,但由于您显式设置了单个字符,因此您必须指定哪个字符。

回答by AnatolyS

what do you think about null-terminated string? Yes, you are right, such strings must be terminated with null:

您如何看待以空字符结尾的字符串?是的,你是对的,这样的字符串必须以 null 结尾:

m_pchString[m_nLength-1] = 0;

m_pchString[m_nLength-1] = 0;

回答by scohe001

You've said that you "get an error stating that strncpy is unsafe to use if i use the null terminator,"but you use strlen, which simply does not workif the string isn't null terminated. From cplusplus:

你说过你“得到一个错误,指出如果我使用空终止符,使用 strncpy 是不安全的”,但是你使用strlen,如果字符串不是空终止,它根本不起作用。从cplusplus

The length of a C string is determined by the terminating null-character

C 字符串的长度由终止空字符决定

My suggestion to you would be to use null or 0 like others are suggesting and then just use strcpyinstead of strncpyas you copy the whole string every time anyways.

我对您的建议是像其他人建议的那样使用 null 或 0,然后每次都使用strcpy而不是strncpy复制整个字符串。