[英]Passing an object as argument in c++
Iam new to c++ trying to learn about namespaces and class.我是 C++ 的新手,试图了解命名空间和类。 I have 2 classes here Both are in different files.
我这里有 2 个类,它们都在不同的文件中。
The first one is第一个是
#include<iostream>
using namespace std;
namespace project{
class Student{
string name,address,enrolmentDate ,usn;
int hours;
public:
Student(char* nusn, char* nname, char* naddress, char* ndate,int nhours){
usn = nusn;
name = nname;
address = naddress;
enrolmentDate = ndate;
hours = nhours;
}
Student(){
getData();
}
void getData(){
std::cout << "Enter Usn,Name,Address,EnrolmentDate,Hours\n";
std::cin >> usn >> name >> address >> enrolmentDate >> hours;
}
string getName(){
return name;
}
string getAddress(){
return address;
}
string getenrolmentDate(){
return enrolmentDate;
}
int getHours(){
return hours;
}
string getUsn(){
return usn;
}
};
}
The Second class is第二类是
#include<iostream>
using namespace std;
namespace project{
class CourseRegistration{
string usn, courseId ;
int hours, grade;
public:
CourseRegistration(project::Student obj, string courseId, int nhours, int ngrade){
usn = obj.getUsn();
this->courseId = courseId;
hours = nhours;
grade = ngrade;
}
};
}
The First class Compiles fine.But the second class gives an error.第一个类编译正常。但是第二个类给出错误。
The error is near that Student Object.错误就在该学生对象附近。
Course.cpp:10:38: error: expected ‘)’ before ‘obj’
CourseRegistration(project::Student obj, string courseId, int nhours, int ngrade){
How Do i Correct It?我该如何纠正?
I do not see any evidence you've included the definition of Student
in "the second file."我没有看到任何证据表明您在“第二个文件”中包含了
Student
的定义。 Your class should be delclared in a header (.h) file with the implementation in a source (.cpp) file.您的类应该在头 (.h) 文件中声明,并在源 (.cpp) 文件中实现。 Your compiler is likely complaining because it doesn't know that
Student
is a class.您的编译器可能会抱怨,因为它不知道
Student
是一个类。
Student.h学生.h
#include<iostream>
using namespace std;
namespace project {
class Student {
private:
string name,address,enrolmentDate, usn;
int hours;
public:
Student( char*, char*, char*, char*, int );
Student();
void getData();
string getName();
string getAddress();
string getenrolmentDate();
int getHours();
string getUsn();
}
}
Student.cpp学生.cpp
#include "Student.h"
namespace project {
Student::Student( char* nusn, char* nname, char* naddress, char* ndate,int nhours ) {
usn = nusn;
name = nname;
address = naddress;
enrolmentDate = ndate;
hours = nhours;
}
Student() {
getData();
}
void getData() {
std::cout << "Enter Usn,Name,Address,EnrolmentDate,Hours\n";
std::cin >> usn >> name >> address >> enrolmentDate >> hours;
}
string getName() {
return name;
}
string getAddress() {
return address;
}
string getenrolmentDate() {
return enrolmentDate;
}
int getHours() {
return hours;
}
string getUsn() {
return usn;
}
}
CourseRegistration.h课程注册.h
#include "Student.h"
#include<iostream>
using namespace std;
namespace project{
class CourseRegistration {
private:
string usn, courseId ;
int hours, grade;
public:
CourseRegistration( Student, string, int, int );
}
}
CourseRegistration.cpp课程注册.cpp
#include<CourseRegistration.h>
namespace project {
CourseRegistration::CourseRegistration( Student obj, string courseId, int nhours, int ngrade ) {
usn = obj.getUsn();
this->courseId = courseId;
hours = nhours;
grade = ngrade;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.