从C#切换到C.我的代码出了什么问题?我需要标题我正在尝试做什么
我已经用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; } 解决方法
首先,您不需要标题用于测试目的.但是如果没有它们,你就无法创建一个真正的程序,因为标题定义了单独编译的程序部分的接口.这就是C/C++的工作方式,没有办法解决.
其次,您必须将public:添加到您的Company类并处理以下错误.它就像C#的东西:你必须使一个函数定义为public void Parse(string)才能从类外部访问它. C方式是 class Foo { public: void Bar(); }; 第三,在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 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 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; } 所以,基本上我们会有员工,公司和主要单位. employee.h中Employee类的定义包含非平凡函数声明.像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 */ 基于Microsoft Visual C的项目通常使用#pragma一次用于相同目的.它更容易使用,但通常不便携. 例如,如果在employee.h中放置Employee :: Hire定义,编译器会将函数代码放在employee.obj和company.obj中(因为company.h包含employee.h).当您尝试在这种情况下链接时,链接器将遇到相同功能代码的2个版本,并将给出错误.内联函数不会编译到单独的实体中,因此不会导致此类错误.模板代码也是如此,它仅在模板实例化时生成.因此,几个翻译单元可能具有相同的非内联模板功能的代码. 程序员可以定义程序的部件边界.如果需要,您可以将公司和员工放入单个翻译单元. VS向导倾向于为每个主要类别制作一个.h / .cpp对.尝试制作一个MFC项目并亲自看看. 这些是基础知识.正如我在上面的评论中提到的,你可以从Stroustrup的“The C programming language”中全面了解这些内容. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |