-1

Im unsure if this is possible as I am very new to C++, and have seen many programmers specifically instructing others not to solely use one cpp file and a few headers.

My question is how can I start/go onto a c++ file from within another? Its hard to explain perhaps this pseudo-code is more understandable?

//Within say main.cpp
int main()
{
    StartProgram(); //Uses a series of commands to run 'StartProgram' like StartProgram();
}

//Within StartProgram()
int maintwo()
{
//Unimportant commands
}

I know this is possible with header files but it seems to throw up errors due to already initialized commands.

Thanks in advance!

user2752347
  • 161
  • 6
  • 16
  • 2
    You can only have one `main`. If you want to split up code you have to create functions or classes. – Appleshell Sep 26 '13 at 10:59
  • 2
    Probably, before you start own projects, you should just take and read a c++ book, if you don't even know how to call a function. Because your questions is really basic. – dhein Sep 26 '13 at 11:02
  • @Zaibis I understand my questsions are basic, could you link me to a book perhaps? – user2752347 Sep 26 '13 at 11:08
  • No I can't, don't take it bad to me, but use google to find a book about c++ as I don't have time or interest in looking it up for you. – dhein Sep 26 '13 at 11:09
  • "teach yourself c++ in 21 days" – James Morris Sep 26 '13 at 14:05

2 Answers2

5

If you're getting issues with duplicate definitions, you need to use an include guard with your header file. For example:

// header.hxx
#ifndef HEADER_HXX_
#define HEADER_HXX_

void maintwo();

#endif

If you don't want to use headers, and you want to access functions from other source files, you can use an extern declaration, like the following:

// file1.cxx
extern void maintwo();

int main()
{
    maintwo();
}

// file2.cxx
#include <iostream>
void maintwo()
{
    std::cout << "maintwo()" << std::endl;
}

But then you have to link them together. If you're using the GCC compiler, you can do so like this:

> g++ -c file1.cxx
> g++ -c file2.cxx
> g++ file1.o file2.o -o my_program

If you want my advice, just use a header file with include guards, and you won't have to worry about multiple declarations / definitions.

bstamour
  • 7,746
  • 1
  • 26
  • 39
1

As you really want to work without header files for prototype usage, you could use functions without prototypes.

(what I advise to avoid as it will break most code if you don't know what you are doing)

Or you can just do the prototype declaration before you define you functions. Something like this:

void function();

int main ()
{
    function();
}

void function ()
{
     //do some stuff
}
dhein
  • 6,431
  • 4
  • 42
  • 74
  • His question was "go onto a c++ file from within another". To answer "Just use the same file" is a bit jerky, don't you agree? ;) – Appleshell Sep 26 '13 at 11:10
  • @Adam S hm. OK you are right, as I jsut ignored the firs part "file" as it made no sense for me :D – dhein Sep 26 '13 at 11:11