在linux下编译简单的C++程序(第一次)
Compiling simple C++ program in linux (first time)
在linux中使用c++编译一个简单程序时出错。共有3个文件:Employee.h , Employee.cpp, and Salary.cpp (main)
。我在Salary.cpp <iostream>, <fstream>, <string>, <stdio.h>, <stdlib.h>
中包含了几个系统头。我使用的唯一原因是用于itoa()
函数。我无法将它编译并读取到某个地方,而"有时是先决条件
我得到的错误是:"Salary.cpp:195:47:error:itoa未在此范围中声明"
现在,我已经在全局范围中包含了头,而itoa()只在我包含的文件中使用,所以我不知道为什么会发生这种情况,除了它没有包含正确的系统头之外。我需要在命令行中指定所有的系统头吗?我不太清楚发生了什么事。
编辑:以下是一些源代码。。。我只详细介绍了保持简短所需的内容。错误发生在底部附近的addEmployee()函数中。。。我不知道该怎么写行号。它就在创建一个新的Employee()之后
#include "Employee.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <cstdlib>
void findEmployees(Employee*[], const short);
void addEmployee(Employee*[], short&);
unsigned short parseDataFile(Employee* [], short);
bool insertEmployee(Employee*[], short, Employee*);
int main(int argc, char** argv)
{
//100 employees max, too lazy to create dynamic container
Employee* employeeList[100];
//Parse data file, return the length of the list made
short listLen = parseDataFile(employeeList, 100);
//Employee database is built, run query engine
std::cout << "*************************************************n";
std::cout << "This program lets you search for salaries of employees or input new employee information.n";
std::cout << "*************************************************nnn";
char choice = { 0 };
while (true)
{
std::cout << "Please choose an option: n";
std::cout << "(1) Search for employee salaries.n";
std::cout << "(2) Input new employee datan";
std::cout << "(3) Exitnn";
std::cin >> choice;
switch (choice)
{
case '1':
findEmployees(employeeList, listLen - 1);
break;
case '2':
addEmployee(employeeList, listLen);
break;
case '3':
exit(0);
break;
}
}
return 0;
}
unsigned short parseDataFile(Employee* empList[], short len)
{
//Do stuff
}
void findEmployees(Employee* empList[], const short len)
{
//Do stuff
}
void addEmployee(Employee* empList[], short& len)
{
char first[32] = "";
char last[32] = "";
char salary[32] = "";
char id[32] = "";
bool loop = true;
while (loop)
{
std::cout << "Input Last Name: ";
std::cin.clear();
std::cin.sync();
std::cin.getline(last, 31);
std::cout << std::endl << std::endl;
std::cout << "Input First Name: ";
//Get user name
std::cin.clear();
std::cin.sync();
std::cin.getline(first, 31);
std::cout << std::endl << std::endl;
std::cout << "Input Salary: $";
//Get user name
std::cin.clear();
std::cin.sync();
std::cin.getline(salary, 31);
std::cout << std::endl << std::endl;
Employee* employee = new Employee();
employee->setID( itoa((int)len, id, 10) ); //Set id
employee->setFirstName(first); //Set first name
employee->setLastName(last); //Set last name
employee->setSalary(salary); //Set salary
//Inserts new employee at the end of the list, no real reason to sort the list for this assignment
//I guess I could have used std::vector to make it easy
empList[len] = employee;
++len; //Increment length of the list
char yesNo = { 0 };
std::cout << "Would you like to enter another employee? (Y, N): ";
std::cin >> yesNo;
std::cout << std::endl << std::endl;
switch (yesNo)
{
case 'Y':
case 'y':
//do nothing
break;
case 'N':
case 'n':
loop = false;
break;
}
}
}
Linux不提供itoa
实现。使用C++11实现相同行为的最佳方法是以以下方式使用std::to_string
:
std::string tmp = std::to_string(1);
如果您使用的是较旧的C++版本,则可以使用字符串流:
std::stringstream tmpSS;
tmpSS << 1;
std::string tmp = out.str();
编辑:在提供的示例中,您还需要调用std::string
的c_str()
方法:
employee->setID( (char*) tmp.cstr() ); //Set id
其中tmp
是前面的选项之一。
此函数未在ANSI-C中定义,也不是C++的一部分,但某些编译器支持。
因此,问题似乎是你的编译器Windows不支持itoa函数。
不确定问题出在哪里(显示部分代码?)。itoa
是C++中的一个非标准函数。
如果您想在C++中将整数转换为字符串,那么您可以在C++11中使用std::to_string函数。
- 如果我们谈论的是
itoa()
,简单的答案是你不应该使用这个函数。它甚至不属于标准的C库。当然,它与C++无关 - 如果您仍然想使用C风格,请使用适当的格式化功能,如
snprintf()
或sprintf()
- 如果您真的想要C++并且支持C++11,请使用
std::to_string()
- 如果您不支持C++11,但想成为C++本机,可以使用str::ostringstream之类的东西
在没有C++11 的C++情况下的2种方法
最后一个可以实际包装在类/方法中的方法示例:
int a = 13;
stringstream ss2;
ss2 << a;
string str2 = ss2.str();
替代方案,类似于(警告,实际上它有点脏,请使用上面的方法,除非你非常需要它):
int Number = 123;
string String = static_cast<ostringstream*>( &(ostringstream() << Number) )->str();
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 我必须构建我的项目 2 次,第一次失败,因为它无法打开库
- 是什么导致我的循环在第一次迭代中运行得更慢
- QLibrary 函数在第一次调用时工作缓慢
- 第一次尝试使用new动态创建结构数组,程序挂起没有错误
- C++程序在第一次尝试时会给出垃圾,但如果它捕获异常并重试,则会给出适当的值
- C++程序有语法错误,第一次使用模块
- 我的 SDL2 应用程序仅在第一次键盘输入后出现严重延迟
- 第一次运行程序时出现损坏的双链表错误,后续运行正常
- 程序不接受任何输入.第一次输入后,它会自动关闭
- boost::async_connect 上的 ASIO 完成处理程序在第一次失败后再也没有调用过
- 我的使用地图的程序在第一次输入后崩溃
- SQLite C++ 程序值仅在第一次插入第二次不插入
- 应用程序在第一次strcat_s崩溃
- 程序在第一次运行时正确地找到了最大值和最小值,但如果我再次运行,它会给我错误的答案
- 在linux下编译简单的C++程序(第一次)
- 在正常应用程序中,当信号量第一次由服务创建时,它被拒绝访问
- C++程序要求我输入两次,即使变量第一次被分配
- 第一次制作了一个跨越几个类的程序,但我在函数调用方面遇到了问题
- 为什么这个程序在第一次循环后崩溃