Collect2:错误:ld返回1退出状态 [英] Collect2: error: ld returned 1 exit status

查看:148
本文介绍了Collect2:错误:ld返回1退出状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的头文件:

This is my header file:

#include <iostream> 
#include <string.h>
#include <string>

using namespace std;

class Tournamentmember
{
protected: 
	char fname[25];
	char lname[25];
	char dob[25];
    static string loc;

public:
	Tournamentmember();
	~Tournamentmember();
	void setFName(char n[]);
	void setLName(char ln[]);
	void setBirth(char b[]);
    void setLocation(string);
	char* getBirth();
	char* getFName();
	char* getLName();
    string getLocation();
};


inline Tournamentmember::Tournamentmember()
{
	cout<<"This is the default constructor"<<endl;
}

inline Tournamentmember::~Tournamentmember()
{
	cout<<"Deconstructor"<<endl;
}

inline void Tournamentmember::setFName(char n[])
{
	strcpy(fname,n);
}

inline char* Tournamentmember::getFName()
{
	return fname;
}

inline void Tournamentmember::setLName(char ln[])
{
	strcpy(lname,ln);
} 

inline char* Tournamentmember::getLName()
{
	return lname;
}

inline void Tournamentmember::setBirth(char b[])
{
	strcpy(dob,b);
}

inline char* Tournamentmember::getBirth()
{
	return dob;
}

inline void Tournamentmember::setLocation(string l)
{
    loc = l;
}
inline string Tournamentmember::getLocation()
{
    return loc;
}



这是我的test.cpp文件我没有cpp文件来放置构造函数和方法(setter和getter),因为我使用Inline


This is my test.cpp file I dont have a cpp file to put the Constructor and methods(setter and getter) since I am using "Inline"

#include <iostream> 
#include "TMember.h"

using namespace std;

int main()
{
	int number;
	char name[25];
	char lname[25];
	std::string location;
	char birth[25];

	Tournamentmember *m1;

	cout<<"Enter number of Member:"<<endl;
	cin>>number;

	m1 = new Tournamentmember [number];

	
	for(unsigned int i = 0; i < number; i++)
	{
		cout<<"Enter first name:"<<endl;
		cin>>name;
		m1[i].setFName(name);
		cout<<"Enter last name:"<<endl;
		cin>>lname;
		m1[i].setLName(lname);
		cout<<"Enter date of birth (yyyy-mm-dd):"<<endl;
		cin>>birth;
		m1[i].setBirth(birth);
	}

	cout<<"Enter the Location:"<<endl;
	cin>>location;

	cout<<"Number of Members: "<< number<<endl;
	cout<<endl;
	for(unsigned int k = 0; k < number ; k++)
	{
		m1[k].setLocation(location);
		cout<<m1[k].getFName()<<endl;
		cout<<m1[k].getLName()<<endl;
		cout<<m1[k].getBirth()<<endl;
		cout<<m1[k].getLocation()<<endl;
	}


	return 0;
}





错误信息:



$ g ++ TMember .cpp TMember.h -o成员

/tmp/ccb4AHqf.o:TMember.cpp :( .rdata $ .refptr._ZN16Tournamentmember3locE [.refptr._ZN16Tournamentmember3locE] + 0x0):对锦标赛成员的未定义引用:: loc'

collect2:错误:ld返回1退出状态



我尝试过:



我收到此错误消息,当我从字符串位置删除静态时,错误消息消失; - >但我需要用静态作为我的作业



Error Message:

$ g++ TMember.cpp TMember.h -o member
/tmp/ccb4AHqf.o:TMember.cpp: (.rdata$.refptr._ZN16Tournamentmember3locE[.refptr._ZN16Tournamentmember3locE]+0x0): undefined reference to `Tournamentmember::loc'
collect2: error: ld returned 1 exit status

What I have tried:

I am getting this error message, the error message goes away when I remove the static from string location; --> but I need to do it with static for my Assignment

推荐答案

g ++ TMember.cpp TMember.h -o member

/ tmp / ccb4AHqf .o:TMember.cpp :( .rdata
g++ TMember.cpp TMember.h -o member
/tmp/ccb4AHqf.o:TMember.cpp: (.rdata


.refptr._ZN16Tournamentmember3locE [.refptr._ZN16Tournamentmember3locE] + 0x0):未定义引用`Tournamentmember :: loc'

collect2:错误:ld返回1退出状态



我尝试过:



我收到此错误消息,当我从字符串位置删除静态时,错误消息消失; - >但我需要使用静态作为我的作业
.refptr._ZN16Tournamentmember3locE[.refptr._ZN16Tournamentmember3locE]+0x0): undefined reference to `Tournamentmember::loc'
collect2: error: ld returned 1 exit status

What I have tried:

I am getting this error message, the error message goes away when I remove the static from string location; --> but I need to do it with static for my Assignment


问题是,你的静态loc变量是声明的,但是没有实现。



解决方案:

a)如果loc属于Tournamentmember,那么它应该是实例成员。 (你写的那个)

b)但如果不是而不是像TournamentLocation那样属于另一个类。



哈克:

The problem is, that your static loc variable is declared, but NOT implemented.

Solution:
a) If the loc belongs to the Tournamentmember, than it should be instance member. (you wrote that)
b) BUT IF NOT than it belongs in another class like TournamentLocation.

Hack:
string Tournamentmember:loc;





在某些实现对象cpp文件。



implement the object in some cpp File.


这篇关于Collect2:错误:ld返回1退出状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆