簡體   English   中英

代碼在 Visual Studio 中不起作用,但在 C++ shell 和 CodeBlock 中工作,編譯器問題?

[英]Code doesn't work in Visual studio, but work in C++ shell and CodeBlock, compiler issue?

#include <iostream>
#include<string>
using namespace std;

void print_message(char *s) {
    cout << s << "\n";
}
int main()
{
    print_message("hello"); 
    print_message("world");
}

此代碼在 Visual Studio 中不起作用,給出了兩個錯誤:錯誤 C2015:常量中的字符過多,錯誤 C2664:'void print_message(char *)':無法將參數 1 從 'int' 轉換為 'char *' Butut work在 C++ shell 和 CodeBlock 中,編譯器問題?

不允許將字符串文字轉換為char * 你的函數需要接受一個char const *像這樣:

void print_message(char const *s) {
    cout << s << "\n";
}

這是一個演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM