在linux下编译简单的C++程序(第一次)

Compiling simple C++ program in linux (first time)

本文关键字:程序 第一次 C++ linux 编译 简单      更新时间:2023-10-16

在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::stringc_str()方法:

employee->setID( (char*) tmp.cstr() ); //Set id

其中tmp是前面的选项之一。

此函数未在ANSI-C中定义,也不是C++的一部分,但某些编译器支持。

因此,问题似乎是你的编译器Windows不支持itoa函数。

不确定问题出在哪里(显示部分代码?)。itoa是C++中的一个非标准函数。

如果您想在C++中将整数转换为字符串,那么您可以在C++11中使用std::to_string函数。

  1. 如果我们谈论的是itoa(),简单的答案是你不应该使用这个函数。它甚至不属于标准的C库。当然,它与C++无关
  2. 如果您仍然想使用C风格,请使用适当的格式化功能,如snprintf()sprintf()
  3. 如果您真的想要C++并且支持C++11,请使用std::to_string()
  4. 如果您不支持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();