Header, Driver, and .cpp files running together

Hi everyone.

Just wondering if I could get some help with figuring out what's wrong with my program. As of now, I have 3 separate files that I'm trying to run together: a driver, a header file, and a cpp file. I'm just starting, but I want to test of two of my functions work in the driver file (and to see if my program even runs together).

However, when I try to build the driver file, it gives me an "undefined reference" error for the two function calls. What's causing this, and how can I fix it?

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
24
25
26
27
28
29
30
#ifndef tsuPod_h
#define tsuPod_h

#include <string>

using namespace std;


/* FUNCTION - void initTsuPod

 Initialize all the slots to blank and 0 size memory

 input parms - none

 output parms - none
*/

void initTsuPod ();

/* FUNCTION - void showSongList
    * prints the current list of songs in order from first to last to standard output
    * format - slot #, Title, Artist, size in MB (one song per line)
    * print "Empty" for any slots that do not contain a song

input parms -

output parms -
*/

void showSongList ();


.cpp file:
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
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>

#include "tsuPod.h"

using namespace std;

const int NUM_SONGS = 8; // Max number of songs in the tsuPod.
const int MEMORY_SIZE = 25;  //Total memory size of the tsuPod

struct Song
{
   string title;
   string artist;
   int memSize;
};

Song songs[NUM_SONGS];

void initTsuPod ()
{
   Song s;
   s.title = "";
   s.artist = "";
   s.memSize = 0;
   
   for (int i = 0; i < NUM_SONGS; i++)
      songs[i] = s;
}

void showSongList ()
{
   Song s;
   
   for (int i = 0; i < NUM_SONGS; i++)
      cout << ++i << ". " //not sure if this is right, want to show "1. " 
         << "Title: " << s.title 
         << "Artist: " << s.artist 
         << "Memory size: " << s.memSize << endl;
   
}


Driver file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <fstream>

#include "tsuPod.h"

using namespace std;

int main()
{
   initTsuPod();

   showSongList();

   return 0;
}


Any help is greatly appreciated!
Okay, so I solved the error, but I noticed that instead of giving me the
1
2
3
4
1.
2.
3.
etc...


that I wanted, it gave me this:

1
2
3
4
1. ....
3. ....
5. ....
7. ....


How do I fix it?
closed account (48T7M4Gy)
try adding #endif at line 30 in header. Worked for me :-)
Actually, I do have that in my header file. It just didn't transfer over when I copied it. Thanks for the help though!

I also fixed the numbering problem, just set up a count.

Now I have this in the .cpp file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void showSongList ()
{
   Song s;
   int count = 1;

   for (int i = 0; i < NUM_SONGS; i++)
   {
	   cout << count << ". "
         << "Title: " << s.title
         << "Artist: " << s.artist
         << "Memory size: " << s.memSize << endl;

	   count++;
   }
}


But now I'm having trouble printing out the size of the file. I keep getting this:
1
2
3
4
5
6
7
8
1. Title: Artist: Memory size: 1856311652
2. Title: Artist: Memory size: 1856311652
3. Title: Artist: Memory size: 1856311652
4. Title: Artist: Memory size: 1856311652
5. Title: Artist: Memory size: 1856311652
6. Title: Artist: Memory size: 1856311652
7. Title: Artist: Memory size: 1856311652
8. Title: Artist: Memory size: 1856311652


what up with the size?
Last edited on
Nevermind, I fixed that too.

Instead of s.title, s.artist, and s.size, I used songs[i].title, etc.
closed account (48T7M4Gy)
Hmmm my version of your program (ie with the #endif as the only change displays the expected memsize = 0 in all cases.

You'll need to review your changes to the loop in songlist . I'd say you've indexed songlist[] out of its range.
Last edited on
That's actually what I ended up doing and it fixed my problems. Thanks for the feedback!
Topic archived. No new replies allowed.