从C#切换到C++.我的代码出了什么问题?我需要标题我正在尝试做什么?一个文件问题中的类定义

use*_*223 3 c++ compiler-errors header class private-members

我已经用C#编程了几年,因为这是我的第一语言.我正在努力提高我的c ++,因为我将很快开发一些编码的东西.

这段代码出了什么问题:(我知道可能存在很多错误.C++与C#的不同之处在于它所需要的).有人告诉我,我不知道如何在C++中正确声明类,并且我需要使用头来定义我的类.我需要标题吗?这是一个小程序,只是为了测试,想知道是否可以在没有它的情况下完成.丢失的标题是唯一的问题吗?我有一个关于无法在公司中访问Parse的错误,但是当我在公司类名称前添加public时,会引发更多错误.

AUGH!太令人沮丧了.

#include "std_lib_facilities.h"
using namespace std;

class Employee
{
public:
    string screen_name;
    string real_name;
    string current_job;
    int employee_number;
    Employee(int no, string name1, string name2, string current_jobin)
    {
        screen_name=name1;
        real_name=name2;
        employee_number=no;
    current_job=current_jobin;
    }
};

class Project
{
public:
    Vector<Employee> Employees;
    int max_worker_quota;
    int project_id;
    string project_name;
    Project(int no_in,int max_in,string title_in)
    {
        max_worker_quota=max_in;
        project_name=title_in;
        project_id=no_in;
    }
};

unsigned int split(const std::string &txt, vector<std::string> &strs, char ch)
{
    unsigned int pos = txt.find( ch );
    unsigned int initialPos = 0;
    strs.clear();

    // Decompose statement
    while( pos != std::string::npos ) {
        strs.push_back( txt.substr( initialPos, pos - initialPos + 1 ) );
        initialPos = pos + 1;

        pos = txt.find( ch, initialPos );
    }

    // Add the last one
    strs.push_back( txt.substr( initialPos, std::min( pos, txt.size() ) - initialPos + 1));

    return strs.size();
}

class Company
{
Vector<Employee> Employeelist;
Vector<Project> Projectlist;

    void Parse(string input)
    {
        //Case Statements
        vector<string> temp;
        split( input, temp, ' ' );

        if (temp[0]=="S")
        {
            //Add Employee to Company
            Employee myEmployee=Employee(atoi(temp[1].c_str()),temp[2],temp[3],temp[4]);
            Employeelist.push_back(myEmployee);
        }
        else if (temp[0]=="C")
        {
            //Add Project to Company
            Project myProject=Project(atoi(temp[1].c_str()),atoi(temp[2].c_str()),temp[3]);
            Projectlist.push_back(myProject);
        }
        else if (temp[0]=="L")
        {
            //Add Employee to Project list
            //Not Implemented-Find Project by temp[1] which is a int
        }
        else if (temp[0]=="A")
        {
        }
        else if (temp[0]=="D")
        {
        }
        else if (temp[0]=="PS")
        {
        }
        else if (temp[0]=="PC")
        {
        }
    }
};

int main(int argc, char *argv[])
{
string input;
cout<<"Command:: ";
cin>>input;
Company myCompany;
myCompany.Parse(input); //Input is in the format X Name Name etc etc. Arguments separated by spaces

return 0;
}
Run Code Online (Sandbox Code Playgroud)

Pav*_*lev 7

首先,您不需要标题用于测试目的.但是如果没有它们,你就无法创建一个真正的程序,因为标题定义了单独编译的程序部分的接口.这就是C/C++的工作方式,没有办法解决.

其次,您必须添加public:到您的Company类并处理以下错误.它就像C#的东西:你必须定义一个函数public void Parse(string)才能从类外部访问它.C++方式是

class Foo { 
public:
void Bar();
}; 
Run Code Online (Sandbox Code Playgroud)

第三,在C++中定义类定义中的非平凡函数是非常规的(唯一的例外是模板类).那是标题故事的另一面.

好的,这里是基本标题相关内容的简要说明.

程序通常分为单独编译的文件集,即翻译单元.每个单元通常由一个.cpp文件和一个或多个头文件组成(.h).编译这些文件时,您将获得一个二进制.obj文件.该文件包含对象 - 函数代码和初始化全局(和命名空间)对象所需的东西.要创建程序,您需要将一个或多个目标文件传递给链接器.在VS中它发生在幕后.您只需将.cpp文件添加到项目树中,IDE将相应地配置项目依赖项.

这就是你的代码的样子:

//File: employee.h

#ifndef EMPLOYEE_HDR  /* include guard */
#define EMPLOYEE_HDR

#include <string>

using std::string;

class Employee {
public:
Employee (int no, string name1, string name2, string current_jobin);

void PayBonus(int amount);

void Fire(string reason);

int GetSalary() const
{ return m_Salary; }

/*...*/
protected:
int m_Salary;
string m_FirstName;
/* ... */
};

#endif

//File: employee.cpp
#include "employee.h"

Employee::Employee (int no, string name1, string name2, string current_jobin)
{
 //definition
}

void Employee::PayBonus(int amount)
{
 //definition
}

void Employee::Fire(string reason)
{
//definition
}

/* define other non-trivial class functions here */

//File: company.h

#ifndef COMPANY_HDR  /* include guard */
#define COMPANY_HDR

#include <vector>
using std::vector;

#include "employee.h"

class Company {
public:
Company();
void Hire(string name);

void WorldCreditCrunch() //life is unfair
{ FireEveryone(); }

void Xmas(); //pays $5 bonus to everyone

/* ... */

protected:

vector<Employee> m_Staff;

void FireEveryone();

/* ... */
};

#endif

//File: company.cpp

#include "company.h"

Company::Company()
{
 //definition
}

void Company::Hire(string name)
{
     //calculate new employee ID etc
     m_Staff.push_back(Employe( /*...*/));
}

void Company::FireEveryone()
{
    for(size_t i = 0; i < m_Staff.size(); ++i)
         m_Staff[i].Fire();

}

void Company::Xmas()
{
    for(size_t i = 0; i < m_Staff.size(); ++i)
         m_Staff[i].PayBonus(5);
}

/* ... */

//File: main.cpp

#include "company.h"

int main()
{
     Company c;

     c.Hire("John Smith");

     /* ...*/

     return 0;
}
Run Code Online (Sandbox Code Playgroud)

所以,基本上我们会有员工,公司主要单位.Employee类的定义employee.h包含非平凡函数声明.在类GetSalary()中定义了一个简单的函数.它给出了编译器内联它的提示.该employee.cpp包含函数定义的休息;

company.h文件具有#include "employee.h"预处理程序语句.因此,我们可以在类定义及其实现文件(company.cpp)中使用Employee对象.

main.cpp包含程序的入口点.它可以使用Company类,因为它包含"company.h".

如果我们在Employee :: Hire()函数实现中更改某些内容,则只会employee.obj重新编译.这是此类计划组织的主要目的.但是,如果我们更改Employee接口(类定义employee.h),则每个程序单元都需要重新编译.

如果您执行以下操作,则需要包含防护:

#include "employee.h"
#include "company.h"  /* will contain the 2nd inclusion of employee.h which will be 
                        prevented by include guard */
Run Code Online (Sandbox Code Playgroud)

基于Microsoft Visual C++的项目通常#pragma once用于相同的目的.它更容易使用,但通常不便携.

例如,如果放置employee.h编译器中的Employee :: Hire定义,则将函数代码放在employee.obj和company.obj中(导致company.h包含employee.h).当您尝试在这种情况下链接时,链接器将遇到相同功能代码的2个版本,并将给出错误.内联函数不会编译到单独的实体中,因此不会导致此类错误.模板代码也是如此,它仅在模板实例化时生成.因此,几个翻译单元可能具有相同的非内联模板功能的代码.

程序员可以定义程序的部件边界.如果需要,您可以将公司和员工放入单个翻译单元.VS向导倾向于为每个主要类别制作一个.h / .cpp对.尝试制作一个MFC项目并亲自看看.

这些是基础知识.正如我在上面的评论中所提到的,你可以从Stroustrup的"The C++编程语言"中全面了解这些内容.