Sign in to follow this  
Sh3rlok

[C++]simple undefined reference to error

Recommended Posts

Ok, I know I'm gonna kick myself for this one, but I can't seem to figure it out. I'm jsut putting together a real simple C++ program so I can get a grasp of it, they only teach java at university and seeing as most jobs require 2-3 years c++ experience I figured it'd be a good idea. I'm converting a lot of my first/second year programs from java to c++ as practice. Anyway, I made an incredibly simple prgoram, you store some strings in a vector, then print them out, real simple. it compiles fine, however I get issues when linking, specifically:
Quote:
undefined reference to 'ClassRoom::addStudent(std::string)'
and a couple other ones, pretty much every place where I try to call a member function of my class. Here's the code (excuse the lack of comments. this was a quick thing): ClassRoom.h
Quote:
#ifndef CLASSROOM_H_INCLUDED #define CLASSROOM_H_INCLUDED #include <string> #include <iostream> #include <vector> using namespace std; class ClassRoom{ string teacher; vector<string> studentList; public: ClassRoom(); ClassRoom(string teacherName); ~ClassRoom(); void addStudent(string name) ; void printClass(); }; #endif // CLASSROOM_H_INCLUDED
ClassRoom.cpp
Quote:
#include <iostream> #include <vector> using namespace std; ClassRoom::ClassRoom() { studentList = new vector(); teacher = "Default Teacher"; } ClassRoom::ClassRoom(string teacherName) { studentList = new vector(); teacher = teacherName; } ClassRoom::~ClassRoom() { delete studentList; } void ClassRoom::addStudent(string name) { studentList.insert(studentList.end(), name); } void ClassRoom::printClass() { cout << "Teacher: " << teacher << endl; cout << "Students: " << endl; for(int i = 0; i < studentList.size(); i++) { cout << studentList[i] << endl; } cout << " ---- End List --- " << endl; }
Main.cpp
Quote:
#include <iostream> #include <vector> #include "ClassRoom.h" using namespace std; int main() { ClassRoom c;// = new &ClassRoom("teacher 1"); c.addStudent("1"); c.addStudent("2"); c.addStudent("3"); //delete c; return 0; }

Share this post


Link to post
Share on other sites
ClassRoom.cpp needs to include ClassRoom.h

[edit:]

Also, as a matter of style/good design, never place a "using" directive inside a header file. This results in unintentional namespace clashing.

Also I can tell you're coming from Java because you're over using the new operator. Take some time to review stack vs heap allocation.

Share this post


Link to post
Share on other sites
Fixed the include, removed the using in the .h file (had to g o through and std:: string and vector) and I'm still getting the same link error. I'm using codeblocks with MingW compiler if that means anything.

new main.cpp

Quote:

#include <iostream>
#include <vector>
#include "ClassRoom.h"

using namespace std;

int main()
{
ClassRoom c;// = new &ClassRoom("teacher 1");

c.addStudent("1");
c.addStudent("2");
c.addStudent("3");

//delete c;

return 0;
}


change to ClassRoom.cpp
Quote:

#include "ClassRoom.h"
#include <iostream>
#include <vector>

using namespace std;

ClassRoom::ClassRoom()
{
teacher = "Default Teacher";
}

Share this post


Link to post
Share on other sites
You shouldn't be writing:

studentList = new vector();


studentList is not a pointer to a vector, it is just a vector and is automatically initialized when the object is constructed. Additionally, you don't need to delete it as its memory is automatically released when the object is destructed.

As fpsgamer says, you should take same time to learn about the differences between stack and heap allocation, as it's very different from in Java.

Share this post


Link to post
Share on other sites
Did some reading last night. Stack/Heap allocation is definetly something I was already familiar with from a class I took, but it was a nice refresher none-the-less.

However I still get linking errors in my program, could it be because of the way the compilers set up or something?

Share this post


Link to post
Share on other sites
I loaded this code up in Visual Studio 2005 to give it a quick check for you. Firstly, Poita is right, don't new / delete your vectors like that, you need pointers if you -really- want to use new and delete, but you don't need it here.

Secondly, after removing the new/delete, the code compiled, linked, and executed fine for me. This leads me to believe you have a compilier / project issue. My guess is that it's not linking in both .CPP files, which leaves you with the undefined references. I'm not too familier with codeblocks, so I couldn't tell you exactly what to check.

I do believe it uses a project system though, so make sure that both files are part of a project and compile the project - that is, don't just try to compile the files by loading each in the editor, make sure you create a project for them to be part of and add each one to the project.

Hope this helps,

-Rhalin

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this