相关疑难解决方法(0)

不推荐使用从字符串文字到char*的转换

我在代码中不断收到错误"从字符串文字转换为char*".代码的目的是使用指针指针为一个字分配string1和string2,然后将其打印出来.我怎样才能解决这个问题?

这是我的代码:

#include <iostream>
using namespace std;

struct WORDBLOCK
{
    char* string1;
    char* string2;
};

void f3()
{
    WORDBLOCK word;

    word.string1 = "Test1";
    word.string2 = "Test2";


    char *test1 = word.string1;
    char *test2 = word.string2;

    char** teststrings;

    teststrings = &test1;
    *teststrings = test2;

    cout << "The first string is: "
         << teststrings
         << " and your second string is: "
         << *teststrings
         << endl;  
}
Run Code Online (Sandbox Code Playgroud)

c++ string char

24
推荐指数
1
解决办法
3万
查看次数

为什么新的 Visual Studio 将字符串文字作为指向常量的指针?

我为我的班级学生提供了这个构造函数。

student::student( char* rollno ,   char* name , date dob) :
    rollno(rollno), name(name),dob(dob)
{
}
Run Code Online (Sandbox Code Playgroud)

当我student s1( "l1f18bscs0322" , "usama" , { 13,7,1998 }); 在 main 中编写 时,它在我的大学中接受它,我认为是因为它使用 Visual Studio 2013,但是当我使用 Visual Studio 2019 时,它在家里出现错误。它说没有构造函数的实例与参数列表匹配,它采用双倍值引号作为指向常量的指针。将值传递给此构造函数的替代方法是什么?因为即使我将构造函数原型更改为指向指针的常量,它也会给出新的错误,即我无法用常量初始化我的非常量成员。

c++ constants visual-studio

1
推荐指数
1
解决办法
94
查看次数

标签 统计

c++ ×2

char ×1

constants ×1

string ×1

visual-studio ×1