calling class method from member method

I have a class that declares a class (static) method and then declares a member method which calls the class method with certain member vars as arguments. It compiles without error but issues a link time error that indicates it can't find the class method. I can view the .o using nm and indeed the signature of the reference in the member function is different than the signature of the class method. I've been struggling with this for days and haven't found a solution. Can someone please help? I am using clang++11.
Post the smallest amount of code that shows all of the functions in question?
Do you mean like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>

class Test {
public:
    Test(int i, std::string s, const char* cs) : mem1(i), mem2(s), mem3(cs) {}
    void print() { test_func(mem1, mem2, mem3); }
    static void test_func(int m1, std::string m2, const char* m3) 
    {
        std::cout << m1 << m2 << m3 << std::endl;
    }

private:
    int mem1;
    std::string mem2;
    const char* mem3;
};

int main() 
{
    Test t1(5, " Hallo ", "How are you?");
    t1.print();

    return 0;
}
Last edited on
Thats essentially what I am doing except mine is more complicated. Yours builds after I changed m3 to mem3 on line 16. Looking at the output from nm I saw that the end of the mangled name of the class method ended in EPKc but the reference in the instance method ended in PKc. I've actually been looking at that for days but the meaning of the mangle eludes me. After seeing yours build properly I focused on the argument and I have now found a define token that needed () around the expansion. That might be a little better but I'm still getting a beaucoup of link time error messages that I don't understand what I'm being told. Names are changed to protect the innocent:

Undefined symbols for architecture x86_64:
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(char const*, unsigned long)", referenced from:
mcl::class::GetId() const in libmcl.a(thingy.o)
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::append(char const*, unsigned long)", referenced from:
mcl::class::GetId() const in libmcl.a(thingy.o)
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::insert(unsigned long, char const*)", referenced from:
mcl::class::GetId() const in libmcl.a(thingy.o)
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string()", referenced from:
mcl::class::GetId() const in libmcl.a(thingy.o)
"std::__1::to_string(int)", referenced from:
mcl::class::GetId() const in libmcl.a(thingy.o)
"vtable for __cxxabiv1::__class_type_info", referenced from:
typeinfo for mcl::class in TestMain.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"vtable for __cxxabiv1::__si_class_type_info", referenced from:
typeinfo for mcl::thingy in TestMain.o
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
"operator delete(void*)", referenced from:
mcl::class::~class() in TestMain.o
mcl::thingy::~class() in TestMain.o
"operator new(unsigned long)", referenced from:
_main in TestMain.o
"___cxa_bad_typeid", referenced from:
mcl::class::GetId() const in libmcl.a(thingy.o)
"___gxx_personality_v0", referenced from:
mcl::class::GetId() const in libmcl.a(thingy.o)
Dwarf Exception Unwind Info (__eh_frame) in libmcl.a(thingy.o)

The only place I have any strings is in:

const char*
class::GetId() const
{
std::string str = NamingThingy();
str += "_" + std::to_string( GetInstance() );
return str.c_str();
}

And, there are no virtual methods in this class but there are a couple of inlines. And there is no 'new unsigned int' in the main, the only new is creating an instance of my class for testing purposes so I don't know what that message is about.

P.S. I havent figured out how to do the fancy formatting in the little blue boxes yet. I tried html but the preview still shows straight text.

PPS. I was doing fine with C++98 using gnu but so far C++11 using clang is giving me grief.
Last edited on
1
2
3
4
5
6
7
const char*
class::GetId() const
{
std::string str = NamingThingy();
str += "_" + std::to_string( GetInstance() );
return str.c_str();
}


This returns a dangling pointer. Using it will result in undefined behavior. Why aren't you returning a std::string?

Errors without the code they refer to aren't particularly helpful. Is mcl a namespace? Did you #include <string>?

I'm returning a char* because of what the routines calling it need. mcl is a namespace from libmcl.a and yes <string> was included. Should I strdup() it?

Dangling pointer or not that still doesn't explain the link time errors.
Last edited on
> PPS. I was doing fine with C++98 using gnu but so far C++11 using clang is giving me grief.
¿are you building with clang instead of clang++?


> Should I strdup() it?
¿do the function expect a malloc string that they will ultimately free?
1
2
std::string foo = bar.GetId(); //GetId returns an std::string
asdf( foo.c_str() );



> I havent figured out how to do the fancy formatting in the little blue boxes yet.
[code]"write your code here"[/code]
you may also use the <> button on the Format toolbox
Are you sure you're linking against the standard library? On Mac OS at least, linking against the C++ standard library isn't done automatically. You'll need to add in a link-time flag to do so. If you have libc++ installed, try adding -lc++ at link time.

-Albatross
> ¿are you building with clang instead of clang++?

I'm using make which is invoking clang++ with flags set to "-std=c++11 -Wall -O2 -stdlib=libc++" as specified in the docs. The flags are being set from CXXFLAGS and LDFLAGS environment vars. I can compile and link all but one of the c++11 examples on the reference side of this site, I haven't tracked down what is wrong with the example I can't build but I don't see it as relevant to what I'm doing so I've ignored it assuming its something that isn't implemented in clang++ yet. I'm not getting any compile time errors, just link time errors

> ¿do the function expect a malloc string that they will ultimately free?

I think they are expecting names in literal space so that might be a problem later but not a big deal yet.

> "write your code here"
> you may also use the <> button on the Format toolbox

I was trying <code> and </code> but the preview wasn't showing any effect. I also tried the button on the side but that injected a Java Script reference that I didn't understand how to use.

> try adding -lc++ at link time.

Ooo. That works. Make is injecting -lc from I don't know where. I'd like to be able to compile both C and C++ so I guess that means putting -lc in CFLAGS and -lc++ in CXXFLAGS but I have to figure out where make is getting -lc and get rid of that first. I'll check make -p.

Thanks to all, I had no idea what to do with the link errors. Seemed pretty cryptic.
Last edited on
Topic archived. No new replies allowed.