为C++; 我正在努力学习C++,但是我读过或看过的大部分教程和书籍都教你这个……
(我假设与大多数教程一样,它们在开始时都是在win32 console或CLR console中教授代码。在这两种情况下,以下内容都不起作用。) 还是像这样为C++; 我正在努力学习C++,但是我读过或看过的大部分教程和书籍都教你这个……,c++,visual-c++,C++,Visual C++,(我假设与大多数教程一样,它们在开始时都是在win32 console或CLR console中教授代码。在这两种情况下,以下内容都不起作用。) 还是像这样 #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { return 0; } #include "stdafx.h" using namespace System; int main(array<System::String ^> ^args) {
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
return 0;
}
#包括“stdafx.h”
使用名称空间系统;
int main(数组^args)
{
控制台::WriteLine(L“Hello World”);
返回0;
}
老实说,我不知道这些有什么不同,我也不确定我是否应该下载一个旧的编译器来让它工作。如果有人能告诉我这些有什么不同,以及从那里去。这将大有帮助。谢谢
[已编辑]
我试着做一个简单的问候世界。但我得到了错误“系统无法找到指定的路径”。我有一个屏幕截图,显示了错误的样子。当我在构建文件之前清楚地保存文件时,它还表示我的项目已经过时。显然它找不到可执行文件。我转到调试文件夹,没有看到任何.exe文件
[已编辑]
好的,现在当我尝试构建项目时,我得到了以下错误
1>------ Rebuild All started: Project: test, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'test', configuration 'Debug|Win32'
1>Compiling...
1>stdafx.cpp
1>Compiling...
1>test.cpp
1>c:\users\numerical25\desktop\test\test\test.cpp(1) : warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1> Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\users\numerical25\desktop\test\test\test.cpp(6) : error C2653: 'std' : is not a class or namespace name
1>c:\users\numerical25\desktop\test\test\test.cpp(6) : error C2065: 'cout' : undeclared identifier
1>Build log was saved at "file://c:\Users\numerical25\Desktop\test\test\Debug\BuildLog.htm"
1>test - 2 error(s), 1 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
1>----已启动全部重建:项目:测试,配置:调试Win32------
1> 正在删除项目“测试”、配置“调试| Win32”的中间文件和输出文件
1> 编译。。。
1> stdafx.cpp
1> 编译。。。
1> test.cpp
1> c:\users\numerical25\desktop\test\test\test.cpp(1):警告C4627:“#include”:在查找预编译头使用时跳过
1> 将指令添加到“stdafx.h”或重新生成预编译头
1> c:\users\numerical25\desktop\test\test\test.cpp(6):错误C2653:“std”:不是类或命名空间名称
1> c:\users\numerical25\desktop\test\test\test.cpp(6):错误C2065:“cout”:未声明的标识符
1> 生成日志保存在“文件://c:\Users\numerical25\Desktop\test\test\Debug\BuildLog.htm”
1> 测试-2个错误,1个警告
=========全部重建:0成功,1失败,0跳过==========
这是我使用的代码
#include <iostream>
#include "stdafx.h"
int main( )
{
std::cout << "Hello World\n";
return (0);
}
#包括
#包括“stdafx.h”
int main()
{
STD::CUT< P >不确定你要问的是什么。你给出的前两个例子是用VC++编写的有效的C++程序。第三个例子是一个C++/CLI程序,必须用/CLR编译器开关编译(这叫做管理C++)。
编辑:添加更多具体信息(来自下面的评论):
前两个例子是标准(原生)C++(虽然第二个例子有MS专有宏),它们编译成本机代码。第三是C++/CLI(一个“托管”扩展到C++)。C++编译为托管(.NET)代码。只有第三个代码段以任何方式与.NET framework交互。所有这三个代码段都是绝对可构建的,并且可以在VS2008中使用适当的项目运行(不需要命令行)
根据您的最新更新,您可能修改了一些项目属性并更改了一些路径。应用程序正在生成,但当您尝试通过VS运行它时(顺便说一句,您应该使用+F5
),找不到可执行文件(有几种方法可以通过更改或使用各种设置来搞乱这一点)
请注意构建和运行之间的区别。构建是编译和链接源代码的过程。运行是启动生成的可执行文件。您似乎混淆了这两个过程(从您对“…过时”对话框的抱怨来看)。获取“…过时”是正常的对话框,如果您在对项目进行更改(即使更改已保存)后尝试运行而不重新生成。请确保单击“是”。您需要先生成项目,然后才能运行它
我的建议是完全删除您的项目和解决方案。创建一个新的空项目,就像这个现在非常重的线程中的其他地方所建议的那样,并且不要修改任何项目设置。如果这不起作用,则说明有严重问题
另一个编辑:只是为了完成,因为这个问题一直在变化:
正如其他人已经指出的,第一个代码段的最终问题是使用预编译头(PCH)在新的VS C++项目中,默认情况下PCH被打开,它们的目的是在许多实现文件包含相同的头文件时加快编译速度,从而防止编译器对每个编译单元的头文件进行解析。
您有三种选择:
(建议)禁用PCH--项目属性-->配置属性-->C/C++-->预编译头:将“创建/使用预编译头”设置为不使用预编译头。(无需对“stdafx.h”文件或其\include
执行任何操作。)
将常用的#include
s放在“stdafx.h”中。在您的情况下,可以将#include
放在“stdafx.h”中
将您的#include
s放在`#include“stdafx.h”之后。Microsoft要求“stdafx.h”是编译单元中包含的第一个文件
\u tmain
使用\u TCHAR
argv是C运行时允许您处理unicode的方式。如果定义了\u unicode
,则\u tmain
将扩展到wmain
,并且\u TCHAR
参数将是wchar\u t类型。如果未定义\u unicode
,则\u tmain
将扩展到main,这将是ANSI标准
因此,只要没有定义< >代码> UnOcdUs/Cuff>,您所发布的第二个代码段符合标准.< /P> < P>下载并安装在您的系统上。如果代码在Visual C++上不工作,请在DEV-C++(使用GCC编译器)上尝试它。。您可能会得到相同的结果或不同的错误消息。当您收到不理解的错误消息时,请在Internet上搜索该错误消息。
#include <iostream>
#include "stdafx.h"
int main( )
{
std::cout << "Hello World\n";
return (0);
}
int main();
int main(int argc, char* argv[]);
int _tmain(int argc, _TCHAR* argv[]);
int wmain(int argc, wchar_t* argv[]);
int main(array<System::String^>^ args);
int main(int argc, char** argv, char** envp);
int main(int argc, char** argv, char** envp, char** apple);
void main();
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
int main(int argc, char* argv[])
int main(int argc, wchar_t* argv[])
cl main.cpp
main.exe
#include "stdafx.h"
#include <iostream>
#include <iostream>
int main( )
{
std::cout << "Hello World\n";
return (0);
}
#include <iostream>
#include "stdafx.h"
int main( )
{
std::cout << "Hello World\n";
return (0);
}