5

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?

4
  • 9
    Have you been learning about namespaces yet? Commented Oct 26, 2013 at 18:40
  • We have touched upon them a bit but I'm not very familiar with them. Commented Oct 26, 2013 at 18:44
  • C++ has overloading: you can have 2 different functions hello(int) and hello(char). This seems silly, but maybe enough for an assignment. Commented Oct 26, 2013 at 18:47
  • 2
    @anatolyg this is bad practice. Namespaces should be used here. Commented Oct 26, 2013 at 18:56

2 Answers 2

17

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.