So I have 3 classes, matrix.h, matrix.cc, and

shortestPath includes the .h file, which includes the matrix.cc file.

In shortestPath I've tried to call a Get method I wrote in the matrix.cc file, but I can't figure out how to call the function on the object of that class. This is the line of code I have:

char exp = mat.Get(0, 0);

I'm more concerned now with how to call the function on the object. the object of the matrix class is called mat. The parameters specify which character to return.

Dani AI

Generated

: the expression char exp = mat.Get(0, 0); is fine as written — provided three things are true: (1) mat is an object (not a pointer), (2) Get is declared public in the class, and (3) your translation units are organized correctly (you should include the header, not an implementation .cc, in other sources). ’s answer shows the same basic pattern: create an object and call the member with the dot operator.

Minimal checklist and concrete examples

  • Header/implementation separation: put the class declaration (including Get) in matrix.h and the definitions in matrix.cpp. Do not #include "matrix.cpp" inside the header — that commonly causes duplicate-symbol/linker errors when multiple .o files include the header.
  • Object vs pointer vs static:
    • object: Matrix mat(...); then mat.Get(...)
    • pointer: Matrix* p = new Matrix(...); then p->Get(...)
    • static member: Matrix::Get(...)
  • Signature and access: make sure Get(int,int) is declared with the exact parameter types and marked public. If it’s const, call it on a const object or remove const as appropriate.

Small illustrative layout (abbreviated)

// matrix.h
#ifndef MATRIX_H
#define MATRIX_H
class Matrix {
public:
    Matrix(int r,int c);
    char Get(int row,int col) const;
};
#endif
// shortestPath.cpp
#include "matrix.h"
void f() {
    Matrix mat(5,5);
    char c = mat.Get(0,0);   // dot for an object
}

Build both implementation files (example): g++ shortestPath.cpp matrix.cpp -o shortestPath

Troubleshooting pointers

  • "mat was not declared in this scope" -> mat is not defined in that function/file.
  • "no matching function for call" -> check parameter types and constness.
  • Linker multiple-definition errors -> stop including .cpp files; compile and link separately.
  • "Get is private" -> move Get into the public: section.

Follow those rules and the call will work.

The Header (test.h)

//Donot include the cc
class test{
public:
void sayHello();
};

the test.cc

#include <test.h>
#include <iostream>
using namespace std;
void test::sayHello()
{
   cout<<"Hello\n";
}

the target class(target.cc)

#include <test.h>
class target{
public:
void callTest()
{
  test t;
  t.sayHello();
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.