写新类时 undefined reference to

binsun 2007-04-17 02:58:39
我实现stack这个类的时候,然后简单调用测试push函数,出现如下错误
main.o(.text+0xf0): undefined reference to `stack::push(basic_string<char, string_char_traits<char>, __default_alloc_template<false, 0> > &)'
collect2: ld returned 1 exit status

代码如下
stack.h
1 #include <string>
2 #include <vector>
3
4 using namespace std;
5
6 class stack{
7 public:
10 bool push(string& str);
11 // string pop();
12 // string find(int pos);
13 // int size(){return str_vec.size();}
15
16 private:
17 vector<string> str_vec;
18 };

stack.cc
1 #incldue "stack.h"
2
3 string str;
4
14
15 bool stack::push(string& str){
16 str_vec.push_back(str);
17 return true;
18 }
19 /*
20 string stack::pop(){
21 return str_vec.pop_back();
22 }
23
24 string stack::find(int pos){
25 return str_vec[pos];
26 }*/

main.cc
1 #include "stack.h"
2 #include <fstream>
3 #include <iostream>
4
5 int main(){
6 stack _st;
7 string word;
8 ifstream ifile("./article.txt");
9 if(!ifile)
10 cerr<<"file error"<<endl;
11
12 cout<<"pls input"<<endl;
13 //while(cin>>word){
14 while(ifile>>word){
15 if( !_st.push(word))
16 cout<<"push error"<<endl;
17 }
28 }

freebsd g++
求助
...全文
1099 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
yutaooo 2007-04-18
  • 打赏
  • 举报
回复
明天帮你试一下. 今晚是不行了, 手上只有vs2003
binsun 2007-04-18
  • 打赏
  • 举报
回复
g++ main main.cc stack.cc ?这样写 ??
做鸡真好吃 2007-04-18
  • 打赏
  • 举报
回复
mark~
freshui 2007-04-18
  • 打赏
  • 举报
回复
你的g++写的不对啊

我手上的makefile都是分开写的
你要是编译和链接一块完成, 不是写上-c和-o, 而是一个都不写
直接g++就是完成编译和链接两个过程
binsun 2007-04-18
  • 打赏
  • 举报
回复
恩 按你的命令行 倒是没有问题了,不过我的为什么错了呢??
freshui 2007-04-18
  • 打赏
  • 举报
回复
g++ -c [.c文件] [可选的-I include文件]


g++ [.o文件] [-L 可选的lib库目录] [-l 可选的库文件] -o 输出的可执行文件

分两次不就行了啊
第一个g++叫编译
第二个g++叫链接
yutaooo 2007-04-18
  • 打赏
  • 举报
回复
大哥! 你就不能按我给的命令行写一次啊!

std::不是问题。只不过我习惯了.h不写using namespace ,强迫症啊!
binsun 2007-04-18
  • 打赏
  • 举报
回复
似乎没有错吧 那单独-c 之后
再怎么 -o 下一个命令怎么写
freshui 2007-04-18
  • 打赏
  • 举报
回复
-o和-c同时用么?不对吧

你-c即可
-o是把那鞋.o文件link时用的吧

:)

俺也不怎么用gcc命令, 我觉得应该是这样
binsun 2007-04-18
  • 打赏
  • 举报
回复
把那行去掉了,而且域作用域标识符std::也加了 还是有错误
我Makefile中如果
1 main:main.o
2 g++ main.o -o main
3 main.o:main.cc
4 g++ -o main.o -c main.cc
5 clean: ~~~~~~~没有stack.cc
6 rm -rf *.o
则报错 如故
如果我在makefile中 加上stack.cc
3 main.o:main.cc
4 g++ -o main.o -c main.cc stack.cc
则报错: g++: cannot specify -o with -c or -S and multiple compilations
可是如果我在命令行单独
g++ -o main.o -c main.cc stack.cc
则无任何结果
g++ -o main.o -c main.cc的话 则错误同makefile中相同写法 g++: cannot specify -o with -c or -S and multiple compilations
困惑 难道我环境的问题??
我freebsd gcc-2.95.3_11
无助啊 求助
yutaooo 2007-04-18
  • 打赏
  • 举报
回复
我测了一下。代码风格稍微改动了一下,我想不影响原意的。不过有个疑惑,在你原先的代码中,以下行干什么用的?
3 string str;


如下:

// stack.h
#include <string>
#include <vector>

class stack{
public:
bool push(std::string const & str);
private:
std::vector<std::string> str_vec_;
};

// stack.cc
#include "stack.h"

bool stack::push(std::string const & str) {
str_vec_.push_back(str);
return true;
}

// main.cc
#include "stack.h"
#include <fstream>
#include <iostream>

int main(){
stack _st;
std::string word;
std::ifstream ifile("./article.txt");
if(!ifile) std::cerr << "file error" << std::endl;
std::cout << "pls input" << std::endl;
while(ifile >> word)
if(!_st.push(word)) std::cout << "push error" << std::endl;
}

// command line
[yutao@BUC test]$ g++ -o test_stack main.cc stack.cc


好象没什么问题啊!不理解啊。
下面是我的编译环境信息:
[yutao@BUC test]$ g++ --version
g++ (GCC) 3.4.3 20050227 (Red Hat 3.4.3-22.1)
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

binsun 2007-04-18
  • 打赏
  • 举报
回复
thanks to all, 直接在commond line些对 可是makefile应该怎么写呢 ?请大家指教
同样的东西放makefile就不对了
binsun 2007-04-17
  • 打赏
  • 举报
回复
g++: cannot specify -o with -c or -S and multiple compilations
直接就报这个错误
yutaooo 2007-04-17
  • 打赏
  • 举报
回复
那你命令行上直接写呢?

g++ -o test main.cc stack.cc

手上没有gcc啊.明天到公司才有.
binsun 2007-04-17
  • 打赏
  • 举报
回复
除了这个 其他的呢
nlfd2007 2007-04-17
  • 打赏
  • 举报
回复
20 string stack::pop(){
21 return str_vec.pop_back();
22 }
这个好象也不对吧,pop_back()删除最后一个元素后,返回void,而你这个函数返回的是string
binsun 2007-04-17
  • 打赏
  • 举报
回复
我出去下 待会回来 多谢各位了
binsun 2007-04-17
  • 打赏
  • 举报
回复
恩 错误依旧
yutaooo 2007-04-17
  • 打赏
  • 举报
回复
现在还不对吗?
binsun 2007-04-17
  • 打赏
  • 举报
回复
那应该怎么写??
加载更多回复(10)

64,654

社区成员

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

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