Calling a function

Hi there!
Just started with c++, this is my first programming language.
I'm trying to follow the Pluralsight tutorial with Kate Gregory.
I'm using QT creator as IDE.

I'm trying to call a function but I'm doing something wrong. I'm using QTcreator as IDE.
What I want to do is to call the loop to main from an other .cpp file.
What would be the proper way to call the loop in the loader.cpp to main.cpp?

I'm currently getting

1
2
3
4
[code]
error: 'loader' cannot be used as a function
     int injectload = loader ();
[/code}



==============
head.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#ifndef HEAD
#define HEAD

#include <QCoreApplication>
#include <iostream>
#include <QTest>
#include <QNetworkInterface>

int b;
int c;
int loader;

#endif // HEAD




==============

loader.cpp
--
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
27
#include <QCoreApplication>
#include <iostream>
#include <QTest>
#include "head.h"

int loader ()

{
for (int loadloop=0;loadloop < 5 ;loadloop++)
{
std::cout << "Laddar...  /" << std::endl;
QTest::qSleep(120);
std::system("cls");

std::cout << "Laddar...  -" << std::endl;
QTest::qSleep(120);
std::system("cls");

std::cout << "Laddar...  \\" << std::endl;
QTest::qSleep(120);
std::system("cls");

std::cout << "Laddar...  |" << std::endl;
QTest::qSleep(120);
std::system("cls");
}
}

===============
main.cpp
--
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <QCoreApplication>
#include <iostream>
#include <QTest>
#include <QNetworkInterface>
#include "head.h"

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

    int injectload = loader ();


      return 0;}

==============


Last edited on
Here is how to call the function loader.
loader(argc, argv);

int loader; Delete this. It does nothing and having a variable with the same name as a function is asking for trouble.

Your main.cpp file cannot see the function named loader, though. It needs a function prototype.


You're not ready for what you're trying to do yet. You need to go back and learn the basics of calling functions first.
I'm sorry, i edited some before you posted.
I changed the header file to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef HEAD
#define HEAD

#include <QCoreApplication>
#include <iostream>
#include <QTest>
#include <QNetworkInterface>

int b;
int c;
loader(argc, argv);

#endif // HEAD 



I'm still getting error:

1
2
main.cpp:12:18: error: 'loader' cannot be used as a function
 loader(argc, argv);
I now have no idea what your code looks like. Show us all the code.
Thanks
Oh thanks for your help!
I'm completely new at this. Sorry about that.


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <QCoreApplication>
#include <iostream>
#include <QTest>
#include "head.h"

int b;
int c;
int loader ();


int main(int argc,char** argv)
{
    loader(argc, argv);


      return 0;}



loader.cpp
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
27
28
29
30
#include <QCoreApplication>
#include <iostream>
#include <QTest>
#include "head.h"

int loader ()

{
for (int loadloop=0;loadloop < 5 ;loadloop++)
{
std::cout << "Laddar...  /" << std::endl;
QTest::qSleep(120);
std::system("cls");

std::cout << "Laddar...  -" << std::endl;
QTest::qSleep(120);
std::system("cls");

std::cout << "Laddar...  \\" << std::endl;
QTest::qSleep(120);
std::system("cls");

std::cout << "Laddar...  |" << std::endl;
QTest::qSleep(120);
std::system("cls");
}
}



---------------------
head.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef HEAD
#define HEAD

#include <QCoreApplication>
#include <iostream>
#include <QTest>


loader(argc, argv);

#endif // HEAD





Error:
1
2
head.h:10:7: error: expected constructor, destructor, or type conversion before '(' token
 loader(argc, argv);




1
2
main.cpp:14:22: error: too many arguments to function 'int loader()'
     loader(argc, argv);
Last edited on

I fixed it!..it think. It runs without any compiler error at least.
Thanks Moschops. :D
I did not understand the reason of header file until know.
So it seems to function as a linker-index?


head.h
1
2
3
4
5
6
7
8
9
10
11
#ifndef HEAD
#define HEAD

#include <QCoreApplication>
#include <iostream>
#include <QTest>


int loader();

#endif // HEAD 


loader.cpp
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
27
#include <QCoreApplication>
#include <iostream>
#include <QTest>
#include "head.h"

int loader ()

{
for (int loadloop=0;loadloop < 5 ;loadloop++)
{
std::cout << "Laddar...  /" << std::endl;
QTest::qSleep(120);
std::system("cls");

std::cout << "Laddar...  -" << std::endl;
QTest::qSleep(120);
std::system("cls");

std::cout << "Laddar...  \\" << std::endl;
QTest::qSleep(120);
std::system("cls");

std::cout << "Laddar...  |" << std::endl;
QTest::qSleep(120);
std::system("cls");
}
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <QCoreApplication>
#include <iostream>
#include <QTest>
#include "head.h"


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

    loader();

 return 0;}
Topic archived. No new replies allowed.