Link Error raised when using string pointer...

I am new to the standard class string and pointer. What I am trying to do is using a string pointer to pass a string in to a constructor. But it doesn't work. It seem that the linker regards 'string*' as 'string' and '*' seperately. I am not very good at Eng so I am not sure. Could you kind guys help me find out where I made mistake,plz? Thx.

Here are the code and Error Message:

1
2
3
4
5
6
class LC
{
...
	LC(string*);
...
};


1
2
3
4
5
6
LC::LC(string* ps)
{
...
	line=*ps;
...
}


1
2
3
4
5
6
7
void main(){
	string line1="Hello World!";
	string* ps=&line1;
	LC a(ps);
	cout<<a<<endl;
	return;
}



1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall LC::LC(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *)" (??0LC@@QAE@PAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main

1>C:\Users\Master\Documents\Assignments\Cmpt\128\Code\Chapter9\Debug\Chapter9.exe : fatal error LNK1120: 1 unresolved externals
Last edited on
It is int main() and return 0;, not that void stuff you have there.

Are you linking your two source files? (The one with main() and the one with the definition of the LC constructor.)
@Zhuge

Thank u for replying. But I think whether returning a value to OS from main function dose not make any difference here. Because I have been making main() return nothing in small console apps for months.

Actually,there are three files in my project, one header file which including the declaration of class, one cpp file which is the implement of header file and the main.cpp file.

Here are the full version of header file and constructor implement:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef LC_H_H
#define LC_H_H

class LC
{
public:
	LC();
	LC(string*);
	void CountWords();
	void CountLetter();
	friend ostream& operator<<(ostream&,const LC&);
private:
	string line;
	int word;
	int count[26];
};
#endif 


1
2
3
4
5
6
7
8
9
10
11
LC::LC(string* ps)
{
	for (int i=0;i<26;i++)
	{
		count[i]=0;
	}
	word=0;
	line=*ps;
	CountWords();
	CountLetter();
}
Last edited on
@Zhuge
Thanks a lot. I find the mistake. At the begining of my implement file, I mistakenly wrote '#ifdef' instead '#ifndef'...How stupid I am...
Topic archived. No new replies allowed.