returning an int vector

I've created a class Vertices that returns an int vector that corresponds to a collection of vertex indexes which that Vertices object connects to.

However, I'm getting a compiler error in my Vertices.cpp file. Here is the function:

1
2
3
4
vector<int> Vertices::getToVertices ()
{
    return toVertices;
}


and here is the compiler message:


/home/9/dunsonm/CSE_680/Homework_6                                              % g++ -o Vertices Vertices.cpp
Undefined                       first referenced
 symbol                             in file
main                                /usr/local/lib/gcc-lib/sparc-sun-solaris2.8/3.0.4/crt1.o
ld: fatal: Symbol referencing errors. No output written to Vertices
collect2: ld returned 1 exit status


Can you please tell me where I'm going wrong?

dunsondog109
Last edited on
It's possible that you called some prototype somewhere that wasn't later defined. It seems like you erased parts of the error message, so I can't be certain.

-Albatross
Hey Albatross. Nice to hear from you again:

Here is Vertices.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
  Vertices.cpp

  Created by Matthew Dunson on May 13 2010
*/

#include "Vertices.h"
#include <iostream>
#include <vector>
using namespace std;

Vertices::Vertices (int vertexNumber)
{
    mark = false;
    fromVertex = vertexNumber;
    parent = -1;
    discover_time = 0;
    finish_time = 0;
    vector<int> toVertices;
}

bool Vertices::getMark ()
{
    return mark;
}

int Vertices:: getFromVertex ()
{
    return fromVertex;
}

vector<int> Vertices::getToVertices ()
{
    return toVertices;
}

int Vertices::getParent ()
{
    return parent;
}

int Vertices::getDiscoverTime ()
{
    return discover_time;
}

int Vertices::getFinishTime ()
{
    return finish_time;
}

void Vertices::setMark (bool isVisited)
{
    mark = isVisited;
}

void Vertices::setFromVertex (int theVertex)
{
    fromVertex = theVertex;
}

void Vertices::addVertex (int toVertex)
{
    toVertices.push_back (toVertex);
}

void Vertices::setParent (int parentVertex)
{
    parent = parentVertex;
}

void Vertices::setDiscoverTime (int timeDiscovered)
{
    discover_time = timeDiscovered;
}

void Vertices::setFinishTime (int timeFinished)
{
    finish_time = timeFinished;
}


No I didn't erase any part of the error message.

Hope this helps.

Hmm... if that's the only file you're compiling and you're not creating a library out of it, then I'd like to note that you're missing main().

I'm also assuming that Vertices.h's class declaration doesn't contain any extra members.

-Albatross
Last edited on
Yeah I figured it out. Thanks anyway.

The problem originally was in a file that had main. I thought that the error was in Vertices.cpp.
Instead of compiling

g++ -o detect_cycle detect_cycle.cpp Vertices.cpp

I only did

g++ -o detect_cycle detect_cycle.cpp
You are declaring toVertices as a local variable on line 19. Once the constructor terminates (line 20) the variable is destroyed. (Even if it weren't, it would still not be accessible on line 34, since it is local to the constructor.)

Classes need definitions like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// fooey.hpp

#include <vector>

class fooey
{
private:
    int               an_int;
    std::vector <int> more_ints;

public:
    fooey();
    std::vector <int> get_ints() const;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// fooey.cpp

#include "fooey.hpp"

using namespace std;

fooey::fooey()
{
    an_int = 42;
    more_ints.push_back( 1 );
    more_ints.push_back( 1 );
    more_ints.push_back( 2 );
    more_ints.push_back( 3 );
    more_ints.push_back( 5 );
}

vector <int> fooey::get_ints() const
{
    return more_ints;
}

Hope this helps.
Ah, well then, you are still declaring an extraneous local variable on line 19.

Glad you figured it out.
Topic archived. No new replies allowed.