0

I'm no newbie to code, but I am to Visual Studios. For the life of me, I cannot figure out why I am getting the following syntax errors. The code is refusing to allow me to declare an object Match m = new Match();.

Main.cpp

#include <iostream>
#include <string>
#include <time.h>
#include "Match.h"
#include "stdafx.h"

using namespace std;

const int NUM_TRIALS = 100000;

int main()
{
    Match m = new Match();
    printf("Program begin\n");
    for (int i = 0; i < 200; i++) {
        m = Match();
        printf("%s ... %s\n", m.to_str123().c_str(), m.printStr.c_str());
    }
    printf("Program end.\n");
    return 0;
}

Match.h

#pragma once
#ifndef MATCH_H_
#define MATCH_H_
#include <string>
#include <iostream>
#include <time.h>

using namespace std;

#define HERO_PER_TEAM 3
#define NUM_HERO 10

class Match {
public:
    Match();
    ~Match();

    string to_str123();
    string printStr();

private:
    char teams[HERO_PER_TEAM * 2];
};

#endif

Error Messages

Error   C2065   'Match': undeclared identifier  ConsoleApplication1 
Error   C2146   syntax error: missing ';' before identifier 'm' ConsoleApplication1 
Error   C2065   'm': undeclared identifier  ConsoleApplication1 
Error   C2061   syntax error: identifier 'Match'    ConsoleApplication1
Error   C2065   'm': undeclared identifier  ConsoleApplication1 
Error   C3861   'Match': identifier not found   ConsoleApplication1 
Error   C2065   'm': undeclared identifier  ConsoleApplication1 
Error   C2228   left of '.to_str123' must have class/struct/union   ConsoleApplication1 
Error   C2228   left of '.c_str' must have class/struct/union   ConsoleApplication1 
Error   C2228   left of '.printStr' must have class/struct/union    ConsoleApplication1
Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
Stephen Lasky
  • 417
  • 5
  • 18
  • it can only be C **or** C++, so I'm removing the C tag. – Marcus Müller May 16 '16 at 19:34
  • 4
    Use either `#pragma once` or macro include guards, not both. – Baum mit Augen May 16 '16 at 19:35
  • 5
    `Match m = new Match();` --> `Match* mPtr = new Match();`. or simply `Match m;` – R Sahu May 16 '16 at 19:37
  • 2
    `using namespace std;` in a header can ruin your day in many ways. Read more here: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice – user4581301 May 16 '16 at 19:42
  • `m.printStr.c_str()` should be `m.printStr().c_str()`. That and the pointer confusion wrap up all of the problems except the missing definition of `Match`, which I'm unable to reproduce. – user4581301 May 16 '16 at 19:49
  • Yes the object identifier is what is confusing the hell out of me. I rarely us VS and when I do the weirdest stuff happens to me. – Stephen Lasky May 16 '16 at 19:59
  • ***Error C2065 'Match': undeclared identifier ConsoleApplication1*** is caused by the following: `#include "stdafx.h"` must be the first non comment / non blank line of your cpp file. – drescherjm May 16 '16 at 20:33

2 Answers2

3

You are using new to assign a value to a non-pointer type. If you want a pointer you can use:

Match* m = new Match();

otherwise, just declare it like this:

Match m;

Since m is not recognized as an object you are getting all of the other errors too.

Also you should be able to use #pragma once in place of standard include guards.

Nathan J
  • 195
  • 10
1

The new operator returns a pointer to an initialized object, using the given constructor. What you're doing here is java syntax. To do it properly you have to create a pointer to an object of that type: Match *m = new Match();. Then instead of using m.printStr you'd use m->printStr and you must not forget to delete the memory allocated using delete m. Or you could simply allocate it on the stack using Match m(); or Match m = Match(). Then you can still use the form m.printStr and you don't need to worry about deleting the memory.

Adrian Jałoszewski
  • 1,695
  • 3
  • 17
  • 33