我在 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 was not declared in this scope
现在,我已经将 header 包含在全局范围内,并且 itoa() 仅在我包含的文件中使用,所以除了不包含正确的系统 header 外,我不知道为什么会发生这种情况.我需要在命令行中指定所有系统 header 吗?我不太确定发生了什么。
编辑:这里是一些源代码...我只是扩展了保持简短所需的内容。错误发生在底部附近的 addEmployee() 函数中……我不确定我是否知道如何放置行号。就在create new 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 << "*************************************************\n\n\n";
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 data\n";
std::cout << "(3) Exit\n\n";
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
是前面的选项之一。
关于c++ - 在 linux 中编译简单的 C++ 程序(第一次),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20167577/