Switching from C# to C++. What's wrong with my code? do I NEED headers for what I'm trying to do? Class definitions within one file issue -
Switching from C# to C++. What's wrong with my code? do I NEED headers for what I'm trying to do? Class definitions within one file issue -
i've been programming in c# few years now, first language. i'm trying brush on c++ because working on coded in that.
what wrong code: (i know there might lot of things wrong. c++ different c# in needs). told me don't know how declare classes correctly in c++, , need define classes using headers. need headers? little programme test , know if can accomplished without it. , missing headers issue here? had error not beingness able access parse in company, when add together public in front end of company class name, throws more errors.
augh! frustrating.
#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 together lastly 1 strs.push_back( txt.substr( initialpos, std::min( pos, txt.size() ) - initialpos + 1)); homecoming 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 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 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 project list //not implemented-find project temp[1] 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 in format x name name etc etc. arguments separated spaces homecoming 0; }
first of all, don't need headers test purposes. can not create real programme without them, because headers define interface of separately compiled programme parts. that's way c/c++ work, no way around.
second, have add together public:
company
class , deal next errors. c# stuff: have create function defined public void parse(string)
able access outside class. c++ way is
class foo { public: void bar(); };
third, unconventional in c++ define non-trivial functions within class definition (the exception beingness template classes). that's other side of headers story tho.
ok, here brief explanation of basic header-related stuff.
program divided set of separately compiled files, translation units. each unit consist of 1 .cpp
file , 1 or more header files (.h
). when these files compiled getting single binary .obj file. file contains objects - code functions , stuff needed initialize global (and namespace) objects. create programme need pass 1 or more object files linker. in vs happens behind scene. add together .cpp file project tree , ide configure project dependencies accordingly.
this how code may like:
//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 { homecoming 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 unfair { fireeveryone(); } void xmas(); //pays $5 bonus /* ... */ 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 = 0; < m_staff.size(); ++i) m_staff[i].fire(); } void company::xmas() { for(size_t = 0; < m_staff.size(); ++i) m_staff[i].paybonus(5); } /* ... */ //file: main.cpp #include "company.h" int main() { company c; c.hire("john smith"); /* ...*/ homecoming 0; }
so, gonna have employee, company , main units. definition of employee class in employee.h
contains non-trivial functions declaration. simple function getsalary()
defined right within class. gives hint compiler inline it. employee.cpp
contains rest of function definitions;
the company.h
file has #include "employee.h"
preprocessor statement. may utilize employee objects in class definition , in implementation file (company.cpp
).
the main.cpp
contains programme entry point. able utilize company class cause includes "company.h".
if alter in employee::hire() function implementation, employee.obj
recompiled. main purpose of such programme organization. if alter employee interface (class definition in employee.h
) every programme unit require recompilation.
include guards needed in case this:
#include "employee.h" #include "company.h" /* contain 2nd inclusion of employee.h prevented include guard */
projects based on microsoft visual c++ utilize #pragma once
same purpose. easier utilize not portable.
if put, example, employee::hire definition in employee.h
compiler set function code in both employee.obj , company.obj (cause company.h
includes employee.h
). when seek link in such situation linker encounter 2 versions of same function code , give error. inline functions not compiled in separate entities , don't cause such error. same goes template code generated when template instantiated. so, several translation units may have code same non-inline template functions.
it programmer define parts boundaries of program. may set company , employee single translation unit, if want. vs wizards tend create .h/.cpp pair each major class tho. seek create mfc project , see yourself.
these basics. mentioned in comment above, can total image of such stuff stroustrup's "the c++ programming language"
c++ class header compiler-errors private-members
Comments
Post a Comment