Visual Studio最近对我发疯了,当我所做的只是一个简单的cout时,它给了我主题上的错误......
法典:
// Lang.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main{
cout << "hi";
}
int main{ cout << "hi"; }
由于可以使用{}
语法C++初始化对象,因此编译器可能会将此代码解释为尝试创建一个名为 main
的全局int
变量,该变量使用 std::ostream::operator<<
的结果初始化,这是一个返回对std::ostream
本身的引用的成员函数。
就好像你写过:
double some_variable { cout << "hi" }
或:
double some_variable { cout }
std::ostream
实际上是std::basic_ostream<char, std::char_traits<char>>
.并且该类型与int
不兼容。
唯一奇怪的是,为什么"hi"
之后的;
不会立即导致编译器停止尝试;但是你没有说你正在使用哪个编译器版本和哪些选项。
无论如何,所有这些事实最终都会导致错误消息:
no suitable conversion function from “std::basic_ostream<char, std::char_traits<char>>” to “int” exists
并在:
此外,"hi"后面的分号突出显示,并显示"预期为 }"
解决方案:使main
成为一个函数:
int main() {
cout << "hi";
}