我的程序可以在 Visual Studio 中运行,但不能在其他 IDE 示例 AWS Cloud 9 中运行

Posted

技术标签:

【中文标题】我的程序可以在 Visual Studio 中运行,但不能在其他 IDE 示例 AWS Cloud 9 中运行【英文标题】:My programs works in Visual Studio but not in other IDE example AWS Cloud 9 【发布时间】:2020-07-29 00:43:32 【问题描述】:

我在 Visual Studio 中创建了我的程序,它运行良好,输入/输出等都是正确的。当我在 AWS Cloud 9 或 Mirmir 中运行它时,我收到相同的错误消息,这是什么意思?我应该如何修改它以在 AWS 中工作?我认为 C++ 代码适用于所有支持 C++ 的 IDE。错误是:

    在构造函数“Departments::Departments(int, char*, char*)”中: 错误:未在此范围内声明“strcpy_s” strcpy_s(部门名称,部门名称); ^

    在构造函数'Employee::Employee(int, char*, double, double, int)'中: 错误:未在此范围内声明“strcpy_s” strcpy_s(emploeename, nameOfTheEmployee);

     #include <iostream>
     #include <string>
     #include <sstream>
     #include <fstream>
    
     using namespace std;
     const int SizeForString = 100;
     char deptartmentRecordFileName[100] = "departments.dat ";
     char employeeRecordFileName[100] = "employees.dat";
    
    
    
     struct  Departments 
         int DepartmentID;
         char Departmentname[SizeForString];
         char DepartmentHeadName[SizeForString];
         Departments()
         
    
         
    
         Departments(int idOfTheDepartment, char nameOfDepartment[], char headNameOfDepartment[])
         
    
             DepartmentID = idOfTheDepartment;
             strcpy_s(Departmentname, nameOfDepartment);
             strcpy_s(DepartmentHeadName, headNameOfDepartment);
         
    
    
    
    
    
     ;
    
    
    
    
    
    
     struct Employee 
    
    
         int employeeID;
         char emploeename[SizeForString];
         double employeesalary;
         double employeeage;
         int employeeDepartmentID;
         Employee() 
         
         
    
    
         Employee(int idOfTheEmployee, char nameOfTheEmployee[], double salaryOfTheEmployee, double ageOfTheEmployee, int departmentId)
    
         
             employeeID = idOfTheEmployee;
             strcpy_s(emploeename, nameOfTheEmployee);
             employeesalary = salaryOfTheEmployee;
             employeeage = ageOfTheEmployee;
             employeeDepartmentID = departmentId;
    
         
    
    
    
    
    
    
     ;
    
    
    
    
    
    
     bool DepartmentIdCheck (int id)
     
         Departments temp;
         ifstream myfile1(deptartmentRecordFileName, ios::in | ios::binary | ios::app);
         if (!myfile1)
         
             cout << "Error To Open File.\n";
             return false;
         
         while (myfile1.read((char*)&temp, sizeof(Departments)))
         
             if (temp.DepartmentID == id)
             
                 myfile1.close();
                 return true;
    
             
         
         myfile1.close();
         return false;
    
     
    
    
    
    
    
     bool checkIDEmplyee (int id)
     
         Employee temp;
         ifstream myfile1(employeeRecordFileName, ios::in | ios::binary | ios::app);
         if (!myfile1)
         
    
    
             cout << "Error To Open File.\n";
             return false;
    
         
         while (myfile1.read((char*)&temp, sizeof(Employee)))
         
             if (temp.employeeID == id)
             
                 myfile1.close();
                 return true;
    
             
         
         myfile1.close();
         return false;
    
     
    
    
    
    
    
     void writeDepartmentData(Departments temp)
     
    
         ofstream myfile1(deptartmentRecordFileName, ios::out | ios::binary | ios::app);
         if (!myfile1)
         
             cout << "Error To Open File.\n";
             return;
    
    
         
    
         myfile1.write((char*)&temp, sizeof(Departments));
         myfile1.close();;
     
    
    
    
    
     void writeEmployeeData(Employee temp)
     
         ofstream myfile1(employeeRecordFileName, ios::out | ios::binary | ios::app);
         if (!myfile1)
         
    
             cout << "Error To Open File.\n";
             return;
         
    
         myfile1.write((char*)&temp, sizeof(Employee));
         myfile1.close();
    
    
    
     
    
    
    
    
    
     void createReport()
     
         double totalSalary = 0;
         Departments depTemp;
         ifstream myfile1(deptartmentRecordFileName, ios::in | ios::binary | ios::app);
         if (!myfile1)
         
    
    
             cout << "Error To Open File.\n";
             return;
    
         
    
    
    
    
    
         while (myfile1.read((char*)&depTemp, sizeof(Departments)))
         
    
             Employee empTemp;
             ifstream myfile2(employeeRecordFileName, ios::in | ios::binary | ios::app);
             while (myfile2.read((char*)&empTemp, sizeof(Employee)))
             
    
    
                 if (!myfile1)
                 
                     cout << "Error To Open File.\n";
                     return;
                 
    
    
    
                 if (depTemp.DepartmentID == empTemp.employeeDepartmentID)
                 
                     totalSalary += empTemp.employeesalary;
                 
             
    
             cout << "Dept       : " << depTemp.Departmentname << endl;
             cout << "Total Salary : $" << totalSalary << endl << endl;
    
    
             myfile2.close();
             totalSalary = 0;
         
         myfile1.close();
    
     
    
    
    
    
    
     int main() 
        
         int choice = 0;
    
         while (choice != 4) 
         
             cout << "\nHuman Resources Menu";
    
             cout << "\n1. Create Department"
                 "\n2. Create Employee"
                 "\n3. Display Salary Report"
                 "\n4. -- Quit -- " << endl;
    
             cout << "Please make a selection : ";
             cin >> choice;
    
             if (choice == 1)
             
                 int deptIdInput;
                 char nameDeptInput[SizeForString];
                 char headNameInput[SizeForString];
    
                 cout << "Please Enter Department Details:" << endl;
                 cout << "Department ID : ";
                 cin >> deptIdInput;
    
    
    
                 bool IdExit = DepartmentIdCheck (deptIdInput);
    
    
    
    
                 if (IdExit)
                 
                     cout << "Value must be unique!" << endl;
                     continue;
    
                 
    
                 cout << "Department Name : ";
                 cin >> nameDeptInput, SizeForString;
                 cout << "Head of Department : ";
                 cin >> headNameInput;
                 Departments d(deptIdInput, nameDeptInput, headNameInput);
                 writeDepartmentData(d);
    
             
    
    
    
    
    
             else if (choice == 2)
             
                 int idEmploy;
                 char nameEmploy[SizeForString];
                 double salaryEmploy;
                 double ageEmploy;
                 int departmentIdForEmploy;
    
                 cout << "Please Enter Employee Details:" << endl;
                 cout << "Employee ID : ";
                 cin >> idEmploy;
    
                 bool IdExit = checkIDEmplyee (idEmploy);
    
    
                 if (IdExit)
                 
                     cout << "Value must be unique !" << endl;
                     continue;
    
                 
    
    
    
                 cout << "Employee Name : ";
                 cin >> nameEmploy;
                 cin.ignore();
    
                 cout << "Salary: $";
                 cin >> salaryEmploy;
    
                 cout << "Age : ";
                 cin >> ageEmploy;
    
                 cout << "Department ID : ";
                 cin >> departmentIdForEmploy;
    
    
    
    
                 bool foundId = DepartmentIdCheck (departmentIdForEmploy);
    
                 while (!foundId)
                 
                     cout << "Please enter a valid department ID  : ";
                     cin >> departmentIdForEmploy;
                     foundId = DepartmentIdCheck (departmentIdForEmploy);
                 
                 Employee e(idEmploy, nameEmploy, salaryEmploy, ageEmploy, departmentIdForEmploy);
                 writeEmployeeData(e);
             
             else if (choice == 3)
             
                 createReport();
             
             else if (choice != 4)
             
                 cout << "Please enter a valid choice" << endl;
    
             
    
         
      
    

【问题讨论】:

您可以使用strcpy,但在Visual Studio 中定义_CRT_SECURE_NO_WARNINGS 【参考方案1】:

您的 Visual Studio IDE 可能已经为您加载了其他 IDE 没有的库。看起来strcpy 是 库的一部分。您需要#include &lt;cstring&gt; 才能使用它。

【讨论】:

【参考方案2】:

Visual Studio 在 Windows 上运行,而 Cloud9 在 linux 上运行。他们使用不同的编译器和不同的标准库。

【讨论】:

以上是关于我的程序可以在 Visual Studio 中运行,但不能在其他 IDE 示例 AWS Cloud 9 中运行的主要内容,如果未能解决你的问题,请参考以下文章

无法在visual studio中使用指针和fstream运行程序

visual studio 2010怎么运行程序?

当我的应用程序已经运行时,如何在 Visual Studio Code 中运行独立的 dart 文件?

如何在visual studio2008中创建,编译和运行C++程序,

无法从特权运行的 Visual Studio 2010 拖放到调试的应用程序运行

在 Visual Studio 之外启动时程序运行速度较慢