0

Currently very new to C++, studying it as part of my programming class.

From what i'm aware, the difference between structs and classes in C++ is that by default, class members are private, and struct members are public.

I wanted to try this out for myself, and wrote a small program in CodeBlocks:

Main.cpp

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

using namespace std;

int main()
{
    TestStruct ts();
    ts.print();
    return 0;
}

StructTest.h

#ifndef TESTSTRUCT_H
#define TESTSTRUCT_H

#include <iostream>

struct TestStruct
{
    TestStruct(){}
    virtual ~TestStruct(){}
    void print();
};

#endif // TESTSTRUCT_H

And implementation file:

#include "TestStruct.h"

TestStruct::TestStruct()
{
    //ctor
}

TestStruct::~TestStruct()
{
    //dtor
}

void TestStruct::print()
{
    std::cout << "Testing" << std::endl;
}

However, I'm getting a compile time error: error - request for member 'print' in 'ts', which is of non-class type 'TestStruct()'

Am I missing something completely obvious here? I thought it was possible to use a struct exactly like a class.

user3650602
  • 175
  • 2
  • 10

0 Answers0