C++: Vectors: why i get these error? - Functions definitions errors

Pages: 123
see these function:
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
26
HANDLE mutex;

void Main(vector<string> argumentlist, bool AsOpened);

int main (int argc, char **argv)
{
    vector<string> test;
    for (int i=0; i<argc;i++)
    {
        test.push_back(argv[i]);
    }
    mutex = CreateMutex( NULL, TRUE, "MyApp" );

    if ( mutex!=NULL)
    {
        Main(test,true);
    }

    else
    {
        Main(test, false);
    }

    CloseHandle(mutex);
    return 0;
}

why get these error?
"undefined reference to `Main(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, bool)'"
these error don't make sence to me.. can anyone explain?
Last edited on
The linker can't find the definition of the Main function.
the Main function is declared on header file, but it's defined on *.cpp file
after a head pain and what you said i notice my error:

on *.cpp:
1
2
void Main(vector<string> argumentlist, bool IsOpened)
{

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
HANDLE mutex;
void Main(vector<string> argumentlist, bool IsOpened=false);

int main (int argc, char **argv)
{
    vector<string> test;
    for (int i=0; i<argc;i++)
    {
        test.push_back(argv[i]);
    }
     mutex = CreateMutex( NULL, TRUE, "MyApp" );

    if ( mutex!=NULL)
    {
        Main(test,true);
    }

    else
    {
        Main(test, false);
    }

    CloseHandle(mutex);
    return 0;
}

now works and i understand where i did the mistake.
thank you so much for all... thank you
instead create another topic, because the next question is about these code, i must ask here.. sorry.
like you see, the Main() have 2 arguments... can i create another function:
void Main()
for call it, if is defined?
I don't understand the last row but yes you can create another function with the same name as long as the parameters are different.
Yes, you can overload functions with different argument lists. So, go ahead and define void Main().

However, you might want to reconsider naming any function void Main(). It is too similar to int main() that it might confuse the reader. I would recommend you change the name of Main() to something easier to identify.
Last edited on
on last row i did a mistake with arguments\parameters and return.
yes... but i don't understand how can i call it if is defined :(
yes... but i don't understand how can i call it if is defined :(

You clearly know how to call functions, so I don't understand your question. Can you clarify what you're asking, please?
i'm sorry, but see these 2 declared functions(think like the main function with or without arguments):
1
2
void Main(vector<string> argumentlist, bool IsOpened=false);
void Main();

the 1st have arguments the 2nd don't.
now see the 'if':
1
2
3
4
5
6
7
8
9
if ( mutex!=NULL)
    {
        Main(test,true);
    }

    else
    {
        Main(test, false);
    }

my problem is: if i define the Main() without parameters, that 'if' will give me some errors because the Main() with arguments isn't defined.
i need to have 2 Main() functions(1 with arguments and other without).
my problem is that i can't test\call undefined functions.
the original main() function can have arguments or not... can i do functions like these?
if i define the Main() without parameters, that 'if' will give me some errors because the Main() with arguments isn't defined.
This is actually impossible. Either you have a function defined or not. I can't think of a third form.

Show the compilable code where the error occurs.
on header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
HANDLE mutex;
void Main(vector<string> argumentlist={0}, bool IsOpened=false);
void Main();
int main (int argc, char **argv)
{
    vector<string> test;
    for (int i=0; i<argc;i++)
    {
        test.push_back(argv[i]);
    }
    mutex = CreateMutex( NULL, TRUE, "MyApp" );

    if ( mutex!=NULL)
    {
        Main(test,true);
    }
    else
    {
        Main(test, false);
    }
    CloseHandle(mutex);
    return 0;
}

on cpp file:
1
2
3
4
void Main()
{
    cout << "hello";
}

error message:
"undefined reference to `Main(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, bool)'"
like the error that i had before.
maybe the best question would be: the original main() can have arguments or don't. how can i do that?
1. why is int main in a header file?
2.
1
2
void Main(vector<string> argumentlist={0}, bool IsOpened=false);
void Main();

You have default arguments for both arguments, so it's ambiguous which function you're referring to.

get rid of the = {0} part of the first Main prototype.

What you currently have is equivalent to me saying:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void func(int x = 3, int y = 4)
{

}

void func()
{

}

int main()
{
    func(); // Error: which function should be called here? Both are equally valid due to default parameters.
}
Last edited on
like the error that i had before.
Yes. The compiler complains about the function [prototype] on line 2 not the one without parameter.

void Main(vector<string> argumentlist={0}, bool IsOpened=false);

Where did you define it?
Ganado:
1 - it's about my big project that i'm doing.
now see these code:
on header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
HANDLE mutex;
void Main(vector<string> argumentlist={0}, bool IsOpened=false);
int main (int argc, char **argv)
{
    vector<string> test;
    for (int i=0; i<argc;i++)
    {
        test.push_back(argv[i]);
    }
    mutex = CreateMutex( NULL, TRUE, "MyApp" );

    if ( mutex!=NULL)
    {
        Main(test,true);
    }
    else
    {
        Main(test, false);
    }
    CloseHandle(mutex);
    return 0;
}

on cpp file:
1
2
3
4
void Main()
{
    cout << "hello";
}

yes the arguments have default values. they are optional.
but i can't define the function Main() without parameters. i get the undefined error
See my post above^
coder777: i notice that. i, now, understand that error.
but can i create a function like the original main function?
but can i create a function like the original main function?
What do you mean?
theres 2 versions of main function: 1 without parameters and other with parameters
Yes, you can do that with Main as well.

What you can't do is have default values for every argument in the version that has arguments. Because if you do that, the compiler has no way of knowing which version you're trying to call when you do:

Main();

EDIT: Here's a question for you:

If you're happy to have defaults for all the arguments to Main(foo, bar), then why do you also need a second Main() ? What so you want that second function to do, that would be different from calling the first function using all default arguments?
Last edited on
Pages: 123