Module pass the compile but can not us in main()

K

key9

Hi All

I defined a class "RootElement"
And want the class can be inherit tree's root.

The main idea is using std::string as the "config" of the instance
the module pass the compile with the help of kindness guys here,but when I
put it in main() , it can not work
Here's the code

*********************************************//root_element.hpp
#ifndef _ROOTELEMENT_
#define _ROOTELEMENT_

#include <string>

class RootElement{

public:
/* just for operate overload */
virtual
RootElement& operator<< (std::string& str);

public:
/* overloadable function open to user*/
// PS: & means reference , change this will dieectly change to obj

/* in */
//accept input string
int acceptiStr(const std::string& istr);

// check input string is legal
int checkiStr(const std::string& istr);

/* out */
virtual std::string outputoStr();


protected:
/*me and my son can use it ,user can not!*/

// any string , just check it! 0 - OK , 1 - error
virtual
int chkiStr(const std::string& istr);

// str must be correct ,must be error check before using
virtual
int hndValidiStr(const std::string& istr);

protected:

// check single pos , inherit that we can simply define one way in our son
virtual
int chkEachiStr(const std::string& istr);

// give out an way can simply handler string's 1 char
virtual
int hndEachiStr(const std::string& istr);

};

#endif /* _ROOTELEMENT_ */

**************************************//root_element.cpp

#include <iostream>
#include "root_element.hpp"
#include <sstream>

RootElement&
RootElement::
operator<< (std::string& str)
{
std::stringstream Temp;
Temp << str;
acceptiStr(Temp.str ());

}

int
RootElement::
acceptiStr(const std::string& istr)
{
if (chkiStr(istr)){
hndValidiStr(istr);
return 0;
}
else{
return 1;
}
}

int
RootElement::
chkiStr(const std::string& istr)
{

std::string::size_type sz_;
std::string::size_type sz_E;

std::string temp_str;

sz_E = istr.length();

sz_ = 0;

while( sz_ != sz_E ){
temp_str = istr.at(sz_);
if (chkEachiStr(temp_str)){
sz_ ++;
}
else
return 1;
}

return 0;
}

int
RootElement::
hndValidiStr(const std::string& istr)
{
std::string::size_type sz_;
std::string::size_type sz_E;

std::string temp_str;

sz_E = istr.length();

for (sz_ = 0; sz_ != sz_E ; sz_++){
temp_str = istr.at(sz_);
hndEachiStr(temp_str);

}

return 0;

}

int
RootElement::
hndEachiStr(const std::string& istr){
std::cout <<" RootElement: hndEachiStr is running! " << std::endl;

}


//testmain.cpp***************************************************
#include<iostream>
#include<string>
#include "root_element.hpp"
using namespace std;

RootElement re_;

int main(int argc, char* argv[])
{

string str = " This is test sample";
re_ << str;
return 0;
}



Thank you very much!

key9
 
K

kwikius

key9 said:
Hi All

I defined a class "RootElement"
And want the class can be inherit tree's root.

The main idea is using std::string as the "config" of the instance
the module pass the compile with the help of kindness guys here,but when I

Some of your functions don't have proper return values. It would be
interesting to know what compiler and settings you have not to catch
this or at least warn

Some of your functions are declared but not defined AFAICS.

regards
Andy Little
 
K

key9

Some of your functions don't have proper return values. It would be
interesting to know what compiler and settings you have not to catch
this or at least warn

$ g++ -v
Using built-in specs.
Target: i486-linux-gnu
Configured with:
.../src/configure -v --enable-languages=c,c++,java,f95,objc,ada,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --enable-nls --program-suffix=-4.0 --enable-__cxa_atexit
--enable-clocale=gnu --enable-libstdcxx-debug --enable-java-awt=gtk-default
--enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-4.0-1.4.2.0/jre
--enable-mpfr --disable-werror --with-tune=pentium4 --enable-checking=release
i486-linux-gnu
Thread model: posix
gcc version 4.0.3 (Ubuntu 4.0.3-1ubuntu5)

$ g++ -c root_element.cpp

$ ls
root_element.hpp semantic.cache test.cpp
root_element.cpp root_element.o

$ g++ -o test1 test.cpp root_element.o
root_element.o:(.gnu.linkonce.r._ZTV11RootElement[vtable for
RootElement]+0xc): undefined reference to `RootElement::eek:utputoStr()'
root_element.o:(.gnu.linkonce.r._ZTV11RootElement[vtable for
RootElement]+0x18): undefined reference to
`RootElement::chkEachiStr(std::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&)'
collect2: ld returned 1 exit status

and if I declear a function in hpp , am I must defined it in cpp? no matter
it performance a NOP operation?
 
K

kwikius

key9 said:
$ g++ -v
Using built-in specs.
Target: i486-linux-gnu
Configured with:
../src/configure -v --enable-languages=c,c++,java,f95,objc,ada,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --enable-nls --program-suffix=-4.0 --enable-__cxa_atexit
--enable-clocale=gnu --enable-libstdcxx-debug --enable-java-awt=gtk-default
--enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-4.0-1.4.2.0/jre
--enable-mpfr --disable-werror --with-tune=pentium4 --enable-checking=release
i486-linux-gnu
Thread model: posix
gcc version 4.0.3 (Ubuntu 4.0.3-1ubuntu5)

$ g++ -c root_element.cpp

Try g++ -Wall -c myfile.cpp

This should output some warnings and you should modify the code to get
rid of those.
$ ls
root_element.hpp semantic.cache test.cpp
root_element.cpp root_element.o

$ g++ -o test1 test.cpp root_element.o
root_element.o:(.gnu.linkonce.r._ZTV11RootElement[vtable for
RootElement]+0xc): undefined reference to `RootElement::eek:utputoStr()'
root_element.o:(.gnu.linkonce.r._ZTV11RootElement[vtable for
RootElement]+0x18): undefined reference to
`RootElement::chkEachiStr(std::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&)'
collect2: ld returned 1 exit status

and if I declear a function in hpp , am I must defined it in cpp? no matter
it performance a NOP operation?

Yes that is what the "undefined reference" messages are saying. You can
also define them inline in the header, but if used they must be
defined.

regards
Andy Little
 
R

red floyd

key9 said:
Hi All

I defined a class "RootElement"
And want the class can be inherit tree's root.

The main idea is using std::string as the "config" of the instance
the module pass the compile with the help of kindness guys here,but when I
put it in main() , it can not work

What errors are you getting? What results (if any) did you get, and how
did they differ from what you expected?

If you don't provide us that info, how do you expect us to help you?
Here's the code

*********************************************//root_element.hpp
#ifndef _ROOTELEMENT_
#define _ROOTELEMENT_

Read my previous post. This is still illegal. Any identifier with a
leading underscore followed by an upper case letter is reserved to the
impplementation.

[remainder redacted]
 
K

key9

Thank , all folks :)

only little experience of VB coding , just beginner of c/cpp ;
no actually c/c++ coding experience.

I found my mistake exist on grammar and misspell.and stipulation of coding,
just like these 2 lines:
#ifndef _ROOTELEMENT_ <--- what on earth I should use? and why?
#define _ROOTELEMENT_

any document or suggest?
 
R

Ron Natalie

key9 said:
Thank , all folks :)

only little experience of VB coding , just beginner of c/cpp ;
no actually c/c++ coding experience.

I found my mistake exist on grammar and misspell.and stipulation of coding,
just like these 2 lines:


any document or suggest?
Something predictable and unlikely to be used for
something else.

I use
#ifdef INCLUDED_MYINCLUDEFILE_H

where the file name is myincludefile.h
 
R

Ron Natalie

key9 said:
Thank , all folks :)

only little experience of VB coding , just beginner of c/cpp ;
no actually c/c++ coding experience.

I found my mistake exist on grammar and misspell.and stipulation of coding,
just like these 2 lines:


any document or suggest?
Something predictable and unlikely to be used for
something else.

I use
#ifdef INCLUDED_MYINCLUDEFILE_H

where the file name is myincludefile.h
 
B

BobR

key9 wrote in message ...
Thank , all folks :)

only little experience of VB coding , just beginner of c/cpp ;
no actually c/c++ coding experience.

I found my mistake exist on grammar and misspell.and stipulation of coding,
just like these 2 lines:


any document or suggest?

Common practice is to use:

// file: RootElement.h purpose: define class RootElement
#ifndef ROOTELEMENT_H
#define ROOTELEMENT_H
// .....
#endif // #ifndef ROOTELEMENT_H

The people who write the compilers need some variables, and they need
insurance that the end user won't change the values. So, they reserved a
certain nameing convention for their use (leading underscore(s)). If you
never start a name with an underscore, you will not have a conflict (there
are times when you can use underscores, but, until you know when/where it's
best to just not use them.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top