steve graves
steve graves

Reputation: 1

Not sure how to call my class in my main function (compiling)

I have to make a program to sort arrays, but in the process of wanting to try and test some basic logic for my program, I found that I got an error on visual studio saying "expected an identifier" whenever I try to call the class constructors. Im still fairly new to c++ and took a year off so im totally lost.

Ive made sure I have my #define and #ifndef, so im not sure as to what else to try.

// This is the header file//
#ifndef LAB2_H
#define LAB2_H
using namespace std;

class SortedArray {
private:
    int capacity;
    int size;
    int* arr;

public:
    SortedArray(int cap = 10);
    ~SortedArray();
    int setCapacity(int cap);
    int getCapacity();
    int setSize(int siz);
    int getSize();
#endif
// This is the .cpp file for my constructors//
#include "Lab2.h"
#include <iostream>

SortedArray::SortedArray(int cap) { 
    setCapacity(cap); 
}
SortedArray::~SortedArray() {}
int SortedArray::setCapacity(int cap) {
    capacity = cap;
}
int SortedArray::getCapacity() {
    return capacity; 
}
int SortedArray::setSize(int siz) {
    size = siz; 
}
int SortedArray::getSize() {
    return size;
// This is my main function file//
#include "Lab2.h"
#include <iostream>

using namespace std;
int x = 0;
int main() {
    int cap = 0;
    SortedArray.getCapacity(); // I get the error on this line//
}

I expect to just get a running function that can compile The error message is "expected an identifier"

Upvotes: 0

Views: 549

Answers (2)

Ted Lyngmo
Ted Lyngmo

Reputation: 117258

First, you should never do using namespace std; into the global namespace in header files. Any user of the header file would get affected by that. Also, in your header file you do it without having first included anything defining that namespace.

// This is the header file//
#ifndef LAB2_H
#define LAB2_H
using namespace std; // from where? remove this

Your class SortedArray is also missing the end: };.

Your .cpp file does #include <iostream> but doesn't use anything from it. Remove.

Your int SortedArray::getSize() also misses the end: }.

Your setCapacity() and setSize() functions are defined to return int - but does not return anything. Make those void.

In main you try to call a member function on your class. You should call it on an instance:

#include "Lab2.h"
#include <iostream>

// using namespace std; // bad practice

//int x = 0; // unused
int main() {
    int cap = 0;
    SortedArray my_instance{cap};

    std::cout << my_instance.getCapacity() << "\n";
}

Upvotes: 2

Kiran Thilak
Kiran Thilak

Reputation: 463

To call member functions of a class we need to make an object.( or the function must be static ).

#ifndef LAB2_H
#define LAB2_H

class SortedArray {
private:
    int m_capacity;
    int m_size;
    int* m_arr;

public:
    SortedArray(int capacity = 10);
    ~SortedArray();
    void setCapacity(int capacity);
    int getCapacity();
    void setSize(int size);
    int getSize();
};
#endif

In .cpp File

#include "Lab2.h"

SortedArray::SortedArray(int capacity) { 
    setCapacity(capacity); 
}
SortedArray::~SortedArray() {}

void SortedArray::setCapacity(int capacity) {
    m_capacity = capacity;
}
int SortedArray::getCapacity() {
    return m_capacity; 
}
void SortedArray::setSize(int size) {
    m_size = size; 
}
int SortedArray::getSize() {
    return m_size;
}

In main

#include "Lab2.h"

int main()
{
    //Make an Object
    SortedArray oSortedArray;
    //Call member function(s)
    int nCapacity = oSortedArray.getCapacity();

    return 0;
}

Upvotes: 1

Related Questions