Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The same function in two different CPP files. How do I accomplish this?

For my homework, this is my assignment:

Create 5 files. Driver.cpp, f.h, f.cpp, g.h, g.cpp. f and g should implement a function called hello. Driver should call hello from f and g.

Example Output:

hello from f

hello from g

Press any key to continue . . .

I have all these files created, but what I dont understand is how can the same function hello() exist in two files and be called from the driver.cpp file? any help would be greatly appreciated!

edit: The error I get is "fatal error LNK1169: one or more multiply defined symbols found". This is referring to the two hello() functions. How do I fix this?

like image 528
user2056120 Avatar asked Oct 26 '13 18:10

user2056120


2 Answers

Globally visible entities are allowed to have only one definition. Thus, you can't have the same function hello() defined in multiple translation units. There are a few separate approaches how to define equally named functions multiple times:

  1. Overloaded function can have the same name as long as they differ in their arguments in some way. For example, you could have each of the hello() functions take an argument which differs between the different versions (note: I'm not suggesting this approch). For example:

    void hello(bool) { std::cout << "hello(bool)\n"; }
    void hello(int)  { std::cout << "hello(int)\n"; }
    
  2. You can define the names in different namespaces. This makes the fully qualified name actually different, i.e., the conflict is prevented by just using a different scope, e.g.:

    namespace a { void hello() { std::cout << "a::hello()\n"; }
    namespace b { void hello() { std::cout << "b::hello()\n"; }
    
  3. Assuming you call your function from a function in the local file, you can move the function from being globally visible to being only locally visible using the static keyword. Functions with local visibility do not conflict between different translation units. For example:

    // file a.cpp
    static void hello() { std::cout << "a.cpp:hello()\n"; }
    void a() { hello(); }
    
    // file b.cpp
    static void hello() { std::cout << "b.cpp:hello()\n"; }
    void b() { hello(); }
    

Which of these versions your teach is actually after, I don't know. Each one has their use, though, and it is useful to know the different variations.

In case someone claims that for completeness I should have included virtual functions: note that overriding a function is actually also creating an overload (the virtual function in the base and the overriding function differ in the implicity passed object), i.e., the use of virtual functions is already covered.

like image 175
Dietmar Kühl Avatar answered Nov 13 '22 07:11

Dietmar Kühl


You should use namespaces :

In f.h

:

namespace mynamespace {
  void hello(); 
}

In f.cpp

void mynamespace::hello()
{
    /... function definition here
}

In main()

int main()
{
   mynamespace :: hello();   // calls hello defined in f.cpp
}

For a good introduction to namespaces. Namespaces

like image 2
Waqar Avatar answered Nov 13 '22 09:11

Waqar