使用 C++ 编译问题 [关闭]
Posted
技术标签:
【中文标题】使用 C++ 编译问题 [关闭]【英文标题】:Compiling Issue with C++ [closed] 【发布时间】:2019-06-21 00:02:43 【问题描述】:我刚开始学习 C++ 课程,我们的第一个任务是一个简单的代码,它利用了用户提供的变量。当我尝试编译代码时,我不断收到一些语法错误,但根据课程的书(和其他在线资源),我认为我的语法是正确的。有什么见解吗?
编辑:澄清一下,我意识到我的语法是错误的,因为编译器会抛出错误,我只是不确定我错在哪里。感谢我收到的所有帮助。
代码:
//SP2019_Lab1Part2_Key.cpp
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
cout << string(50, '-') << endl;
cout << "SP2019_Aaron Key - OUTPUT OF USING VARIABLES" << endl;
//declares string variable "Word"
char Word;
//Prompts user for word:
cout << "Word: ";
//Waits for input for value of "Word" from standard input (keyboard)
cin << Word;
//Declares integer variable "FirstNumber"
double FirstNumber;
//Prompts user for a number:
cout << "First Number: ";
//Waits for number:
cin << FirstNumber;
//Declares double variable "SecondNumber":
double SecondNumber;
//Prompts user for a number:
cout << "Second Number: ";
//Waits for input:
cin << SecondNumber;
//Adds FirstNumber and SecondNumber:
double SUM = FirstNumber + SecondNumber;
//Calculates Average of FirstNumber and SecondNumber:
double Avg = SUM / 2;
//Gives the average of FirstNumber and SecondNumber:
stringstream calavg;
calavg << "The average of " << FirstNumber << " and " << SecondNumber
<< " is: " << Avg;
cout << char calavg.str();
cout << string(50, '-') << endl;
错误:
Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27026.1 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
SP2019_Lab1Part2_Key.cpp
SP2019_Lab1Part2_Key.cpp(9): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(10): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(10): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(10): error C2086: 'int cout': redefinition
SP2019_Lab1Part2_Key.cpp(9): note: see declaration of 'cout'
SP2019_Lab1Part2_Key.cpp(14): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(14): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(14): error C2086: 'int cout': redefinition
SP2019_Lab1Part2_Key.cpp(9): note: see declaration of 'cout'
SP2019_Lab1Part2_Key.cpp(16): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(16): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(21): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(21): error C2086: 'int cout': redefinition
SP2019_Lab1Part2_Key.cpp(9): note: see declaration of 'cout'
SP2019_Lab1Part2_Key.cpp(23): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(23): error C2086: 'int cin': redefinition
SP2019_Lab1Part2_Key.cpp(16): note: see declaration of 'cin'
SP2019_Lab1Part2_Key.cpp(28): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(28): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(28): error C2086: 'int cout': redefinition
SP2019_Lab1Part2_Key.cpp(9): note: see declaration of 'cout'
SP2019_Lab1Part2_Key.cpp(30): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(30): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(30): error C2086: 'int cin': redefinition
SP2019_Lab1Part2_Key.cpp(16): note: see declaration of 'cin'
SP2019_Lab1Part2_Key.cpp(40): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(40): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(40): error C2371: 'calavg': redefinition; different basic types
SP2019_Lab1Part2_Key.cpp(39): note: see declaration of 'calavg'
SP2019_Lab1Part2_Key.cpp(41): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(41): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(41): error C2086: 'int cout': redefinition
SP2019_Lab1Part2_Key.cpp(9): note: see declaration of 'cout'
SP2019_Lab1Part2_Key.cpp(42): error C2143: syntax error: missing ';' before '<<'
SP2019_Lab1Part2_Key.cpp(42): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
SP2019_Lab1Part2_Key.cpp(42): error C2086: 'int cout': redefinition
SP2019_Lab1Part2_Key.cpp(9): note: see declaration of 'cout'
【问题讨论】:
" 我认为我的语法是正确的。" 编译器告诉你不是。 他们没有教你main
函数吗?您不能只在文件范围内编写代码。
using namespace std;
我正在上一门在线课程,本书的第一章(作业应该与此一致)只讨论了伪代码,以及用伪代码思考的重要性.对于这样的问题更“热门”的在线论坛,您有什么建议吗?
【参考方案1】:
你在这里犯了一些错误:
您必须在int main()
中编写整个主代码
使用 cin 的语法是 cin >>
而不是 cin <<
您不需要将char
放在calavg.str()
之前,因为它已经声明为字符串流
不要使用using namespace std
。这是一种不好的做法,可能会导致库冲突Why is "using namespace std" considered bad practice?
代码:
#include <iostream>
#include <string>
#include <sstream>
int main()
std::cout << "SP2019_Aaron Key - OUTPUT OF USING VARIABLES" << std::endl;
//declares string variable "Word"
char Word;
//Prompts user for word:
std::cout << "Word: ";
//Waits for input for value of "Word" from standard input (keyboard)
std::cin >> Word;
//Declares integer variable "FirstNumber"
double FirstNumber;
//Prompts user for a number:
std::cout << "First Number: ";
//Waits for number:
std::cin >> FirstNumber;
//Declares double variable "SecondNumber":
double SecondNumber;
//Prompts user for a number:
std::cout << "Second Number: ";
//Waits for input:
std::cin >> SecondNumber;
//Adds FirstNumber and SecondNumber:
double SUM = FirstNumber + SecondNumber;
//Calculates Average of FirstNumber and SecondNumber:
double Avg = SUM / 2;
//Gives the average of FirstNumber and SecondNumber:
std::stringstream calavg;
calavg << "The average of " << FirstNumber << " and " << SecondNumber
<< " is: " << Avg;
std::cout << calavg.str();
return 0;
【讨论】:
好的,我将编辑帖子..实际上使用命名空间std更容易被初学者理解,每个编程讲座都在开始时使用该命名空间(至少对我而言) @gameon67 "其实使用命名空间std对于初学者来说更容易理解,每一堂编程课一开始都使用那个命名空间" 值得商榷。在最初的学习阶段之后,改掉使用using namespace std;
的习惯比理解命名空间的概念(不是细节,而是概念本身)更难。然而,大多数讲师往往只是不打扰。相关阅读:Why is “using namespace std” considered bad practice?
已编辑。我同意伙计们。使用命名空间 std 是一个非常糟糕的习惯。我想我刚上大学时的编程课很糟糕
哇,谢谢大家!我不知道 using namespace std - 很多在线参考推荐它,所以我认为它只是一个方便的小功能。
@akey 第一个月学习编程,很方便。但是当你进入更大的项目时,这是很危险的。也请接受我的回答:)以上是关于使用 C++ 编译问题 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章