I am trying to compile my code by using make file. It seems to me that I have a bit problem in my code but it gave me no error except reference problem( referenced from:_main in lab2-f679eb.o )
I am trying to pass the object to function by reference.
This is my general code: In main.cpp:
else if (Integer.getInteger(text_R.getText()) < 0
|| Integer.getInteger(text_R.getText()) > 255)
In other.cpp
ClassName obj
obj.functionName(obj); // passing object to the funcion
In header.h:
FunctionName(ClassName &obj){
cout<<"Please enter first name:"<<endl;
cin>>user.fname;
cout<<"Please enter last name:"<<endl;
cin>>user.lname;
}
The big question is that, do I have a correct structure or not ? what I want to do to pass the object to function not method. When I run the code there is no error but it gave me
Class ClassName
{
public:
std::string fname;
std::string lname;
FunctionName(ClassName& obj);
};
答案 0 :(得分:0)
You're getting a linker error, which means that the compiler was able to compile the program but couldn't find the implementation for one of the member functions you wrote. Notice that it's saying that corpus = [dictionary.doc2bow(text) for text in texts]
is the undefined symbol, meaning that the compiler never found an implementation for this function.
So why is that? Well, in ClassA::Function(Class A&)
, you implemented the following:
other.cpp
This is a free function named ReturnType Function (B&) {
...
}
, which is not the same as the function Function
. This means that the compiler doesn't treat it as a member function, so when it compiles your code it never ends up finding ClassA::Function
.
To fix this, change ClassA::Function
in the .cpp file so that you explicitly indicate it's a member function:
Function
That should resolve your error.
答案 1 :(得分:0)
在这里,经过长时间的努力,我会回答自己。我会更详细地回答我的答案,而不仅仅是需要答案。
这里我将编写几个不同参数传递的函数(按值和引用调用。
这是我的头文件。
#ifndef USER_H
#define USER_H
class User
{
public:
std::string fname;
std::string lname;
std::string num[5];
int age;
};
void Update(User &user);
void View(User &user);
void Create(User &user);
void Favorites(User &user);
#endif
这是我的cpp文件:
#include <iostream>
#include "User.h"
using namespace std;
void
Create(User& user)
{
cout<<"Please enter first name:"<<endl;
cin>>user.fname;
cout<<"Please enter last name:"<<endl;
cin>>user.lname;
cout<<"Please enter age:"<<endl;
cin>> user.age;
cin.ignore(1000,'\n');
}
void
Update(User& user)
{
cout<<"Please enter which attribute to update (First, Last, Age):"<<endl;
string option;
string Age = "Age", First = "First" , Last = "Last";
//cout<<"what you want to update"<<endl;
//cout<<"Please enter which attribute to update (First, Last, Age): "<<endl;
cin>> option;
if(option == First)
{
cout<<"Please enter the new value:"<<endl;
cin>>user.fname;
}
if(option == Last)
{
cout<<"Please enter the new value:"<<endl;
cin>>user.lname;
}
if(option == Age)
{
cout<<"Please enter the new value:"<<endl;
cin>>user.age;
cin.ignore(1000,'\n');
}
}
void
View(User& user)
{
cout<<"First name:"<<user.fname<<endl;
cout<<"Last name:"<< user.lname<<endl;
cout<<"Age:"<<user.age<<endl;
cout<<"Favorite Movies"<<endl;
cout<<endl;
int i;
for( i = 0 ; i< 5 ; i++)
{
cout<<user.num[i]<<endl;
}
}
void Favorites(User& user)
{
cin.ignore(1000,'\n');
cout<<"Please input your new 5 Favorite Movies"<<endl;
int i;
for(i = 0; i < 5 ; i++)
{
//cin>>user.num[i];
//cin.ignore(1000,'\n');
getline (cin, user.num[i]);
}
}
最后这是我的主要功能:
#include <iostream>
#include "User.h"
using namespace std;
int main()
{
User user;
while(true)
{
string option;
cout<<"Please enter a command (Create, Update, View, Favorites, or Quit):"<<endl;
cin>> option;
if(option == "Create")
{
Create(user); //// passing object to the funcion
}
else if(option == "Update")
{
Update(user);
}
else if(option == "View")
{
View(user); //// passing object to the funcion
// setUserName(User &user, string f, string l);
}
else if(option == "Favorites")
{
Favorites(user); //// passing object to the funcion
}
else if(option == "Quit")
{
cout<<"You Exit from the Program "<<endl;
break;
}
else if(option != "Create" || option != "Update" || option != "View" || option != "Favorites" || option != "Quit")
cout<<"INVALID COMMAND"<<endl;
}
return 0;
}