How to Resolve an Undefined Reference Error in C++ When Using SparseMatrix?

  • Comp Sci
  • Thread starter subwaybusker
  • Start date
  • Tags
    C++ List
Otherwise, the compiler will not be able to find the definition of the SparseMatrix constructor and will give you an "undefined reference" error. In summary, make sure to include both the header and implementation files for any classes that you are using in your code to avoid this error.
  • #1
subwaybusker
51
0
Code:
#include <iostream>
#include "recommender.h"
#include <stdio.h>
#include "sparsematrix.h"
using namespace std;

#ifndef RECOMMENDER_H
#define RECOMMENDER_H

class Recommender  {
    private:
        SparseMatrix* ratings;  // pointer to sparse matrix containing ratings
        char** movieNames;  // array of strings containing movie names
        int numUsers;
        int numMovies;
        int numRatings;

    public:
        // ============= FUNCTIONS TO BE IMPLEMENTED =============
        Recommender(char* filename);
        ~Recommender();

        void setNumUsers(int a) { numUsers = a; };
        void setNumMovies(int a) { numMovies = a; };
        void setNumRatings(int a) { numRatings = a; };

        char* getMovieName(int movieID);
        int getNumMovies();

        void sortMoviesByAvgRating(int order, int** sortedMovieIDs);
        float computeMovieSimilarity(int movieID1, int movieID2);
        void sortMoviesBySimilarity(int movieID, int order, int** sortedMovieIDs);
        // ========================================================
};




#endif

SparseMatrix* ratings = new SparseMatrix(numUsers, numMovies);


I'm also getting an "undefined reference error" to the SparseMatrix class in this Recommender file even though I included the header file for SparseMatrix
 
Last edited:
Physics news on Phys.org
  • #2
.Here is the error message:/tmp/ccVzjFxn.o: In function `Recommender::Recommender(char*)':recommender.cpp:(.text+0x54): undefined reference to `SparseMatrix::SparseMatrix(int, int)'collect2: error: ld returned 1 exit statusmake: *** [recommender] Error 1</code>The problem is that you are not including the implementation of the SparseMatrix class.You need to include both the header and implementation files for the SparseMatrix class in your Recommender.cpp file.
 
  • #3
. It's possible that the header file is not properly linked to the implementation file, or that there is an error in the implementation of the SparseMatrix class. I would suggest double checking the implementation of the SparseMatrix class and ensuring that it is properly linked to the header file in the Recommender class. It may also be helpful to run a debugger to identify the specific source of the error.
 

Related to How to Resolve an Undefined Reference Error in C++ When Using SparseMatrix?

1. What is a linked list in C++?

A linked list in C++ is a data structure that consists of a sequence of nodes, where each node contains data and a pointer to the next node in the list. It allows for efficient insertion and deletion of elements in the list, but accessing elements requires traversing through the list from the beginning.

2. How do you implement a linked list in C++?

To implement a linked list in C++, you first need to create a struct or class for the nodes, which should contain a data variable and a pointer to the next node. Then, you can create a class for the linked list that contains a pointer to the head node and functions for inserting, deleting, and accessing elements in the list.

3. What are the advantages of using a linked list in C++?

One advantage of using a linked list in C++ is its dynamic nature, meaning that it can easily grow or shrink in size as needed. It also allows for efficient insertion and deletion of elements, unlike arrays which require shifting elements to make space. Additionally, linked lists can be easily modified without having to change the size of the data structure.

4. What are the disadvantages of using a linked list in C++?

One disadvantage of using a linked list in C++ is that accessing elements in the list can be slower compared to arrays, as it requires traversing through the list. Another disadvantage is that linked lists use more memory than arrays, as each node contains a pointer in addition to the data.

5. How do you traverse a linked list in C++?

To traverse a linked list in C++, you can use a while or for loop to iterate through each node in the list, starting from the head node. To access the data in each node, you can use the arrow operator (->) to access the data variable. You can also use recursion to traverse the list.

Back
Top