头文件总通过不了链接,帮帮忙

yanworld 2011-11-02 12:38:32

//test_main.cpp

int a;

...全文
162 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
iamnobody 2011-11-02
  • 打赏
  • 举报
回复
class string;
int count(const string &str, char ch);

soloopin 2011-11-02
  • 打赏
  • 举报
回复
补充一下: test_main.h 文件名应该改成test_main.cpp。不然会报错。
yanworld 2011-11-02
  • 打赏
  • 举报
回复
那个。。。
一些小原因。。。

上面的是问题。。。
(不会有插楼的现象吧。。。)
yanworld 2011-11-02
  • 打赏
  • 举报
回复

//test_main.h

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

using namespace std;

int main()
{
cout << count("abcda", 'a') << endl;
return 0;
}




//test.h

#ifndef COUNT_H
#define COUNT_H

int count(const string &str, char ch);

#endif




//test_count.cpp

#include "test.h"
#include "string"

int count(const string &str, char ch)
{
int count = 0;
for(string::size_type ix = 0; ix != str.size(); ++ix)
{
if(str[ix] == ch)
++count;
}

return count;
}




VC6.0, win7
错误提示:

e:\c++\cpp__7_header\test.h(4) : error C2143: syntax error : missing ',' before '&'
e:\c++\cpp__7_header\test.h(4) : error C2059: syntax error : '&'
E:\C++\cpp__7_header\test_main.cpp(8) : error C2660: 'count' : function does not take 2 parameters
iamnobody 2011-11-02
  • 打赏
  • 举报
回复
#ifndef __INT_A
#define __INT_A
int a;
#endif
iamnobody 2011-11-02
  • 打赏
  • 举报
回复
#ifndef __INT_A
#define __INT_A
int a;
#else
iamnobody 2011-11-02
  • 打赏
  • 举报
回复
又是这个问题。。。你在别外的文件也声明了这个吧。。加个static试试:
static int a;
soloopin 2011-11-02
  • 打赏
  • 举报
回复

//test_main.cpp

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

using namespace std;

int main()
{
std::string str="abcda";
cout << count(str, 'a')<< endl;
return 0;
}






#ifndef COUNT_H
#define COUNT_H

#include <string>
extern int count(const std::string &, char );

#endif





//test_count.cpp
#include <string>
class string ;
int count(const std::string &str, char ch)
{
int count = 0;
for(std::string::size_type ix = 0; ix != str.size(); ++ix)
{
if(str[ix] == ch)
++count;
}

return count;
}




你看一下是不是满足你的意思了?

编译命令: g++ test_main.cpp test_count.cpp -o test
yanworld 2011-11-02
  • 打赏
  • 举报
回复
是这个问题,提示错误

e:\c++\cpp__7_header\test_main.cpp(7) : error C2664: 'count' : cannot convert parameter 1 from 'char [6]' to 'const class string &'
Reason: cannot convert from 'char [6]' to 'const class string'
Source or target has incomplete type

不是说 const引用可以绑定到不同但相关的类型 吗?
我不明白为什么还会有这个提示。

对了,string class;是一个class声明吗?还没看到那里。。。

麻烦告诉一下,谢谢了。
turing-complete 2011-11-02
  • 打赏
  • 举报
回复
坦白地讲,头文件和链接都不挨着。
「已注销」 2011-11-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 mingliang1212 的回复:]
class string;
int count(const string &str, char ch);
[/Quote]

人家都跟你说了答案了, 楼主还想怎么样? 声明.h里不认识string, 加个前置声明。
yanworld 2011-11-02
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 caddor2011 的回复:]

.h中定义变量,用static int a

或者加上 宏

#ifndef __INT_A
#define __INT_A
int a;
#endif

楼上都说了。。。。

还说你有问题,把所有都贴出来
[/Quote]

4楼,把问题贴在4楼了
caddor2011 2011-11-02
  • 打赏
  • 举报
回复
.h中定义变量,用static int a

或者加上 宏

#ifndef __INT_A
#define __INT_A
int a;
#endif

楼上都说了。。。。

还说你有问题,把所有都贴出来


yanworld 2011-11-02
  • 打赏
  • 举报
回复

#13 soloopin, 兄弟牛逼。

1、
突然发现,想起来,在头文件中,有时候的定义是一定要用这种形式的:
std::string str;


原因在于你需要指定命名空间。我在 test_count.cpp 中,
既没有用 using namespace std; 也没有依照上面所说的那种形式,
所以当然错咯

2、
即使是在 头文件中,如果要使用 非内置类型,
也需要 #include,
所以,test.h 也就错了

下面是我最终的代码:

// test_main.cpp

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

using namespace std; // 命名空间声明

int main()
{

cout << count("abcda", 'a') << endl; //的确,const引用 是可以接受 字符串字面值
return 0;
}



// test.h

#ifndef COUNT_H
#define COUNT_H

#include <string>
int count(const std::string &str, char ch);

#endif



//test_count.cpp

#include "test.h" // 这个添加可以让编译器帮助检查 函数声明是否正确,特意加的。
#include "string"

using namespace std; // 可以像 soloopin兄弟那样,std::string,也可以直接声明。
// 不过,我不知道哪个好,哪个坏。
// 感觉上,还是 soloopin 那样做好。原因:凭感觉。
int count(const string &str, char ch)
{
int count = 0;
for(string::size_type ix = 0; ix != str.size(); ++ix)
{
if(str[ix] == ch)
++count;
}

return count;
}


VC6.0, win7

再谢谢 soloopin :)
yanworld 2011-11-02
  • 打赏
  • 举报
回复
能过编译吗?
提示错误:

E:\C++\cpp__7_header\test.cpp(8) : error C2664: 'count' : cannot convert parameter 1 from 'char [6]' to 'const class string &'
Reason: cannot convert from 'char [6]' to 'const class string'
Source or target has incomplete type

64,632

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧