This question already has answers here:
Why does stdafx.h work the way it does?
(4 answers)
Closed 5 years ago.
I'm facing a really stupid and infuriating problem, I was following a video on how to make a roguelike, I decided to stop after knowing how to print a level to the screen and actually start coding, then I got multiple errors on seamingly perfect code, I blammed the constructors so I decided to make a new project on visual studio to test the most stupid case, one main fuction, one clase with one constructor that does nothing, and I leaved all the work of making the class and the constructor to visual studio, I still got the goddammned issue, here's the error on that simplified version of the code, I would show the rogue like code but I would have to translate to english al the errors myself, and most of them have nothing to do with the actual error, because mostly they're complaining about missing ; in places where either there shouldn't be a ; or there alredy is one.
here's the code
main.cpp:
#include "stdafx.h"
int main()
{
return 0;
}
test.h
#include "stdafx.h"
class test
{
public:
test();
};
test.cpp
#include "test.h"
#include "stdafx.h"
test::test(){
}
yet with that simple auto-generated code, visual studio still complaints and gives me this error messages
c4430 missing type specifier - int assumed. Note: C++ does not support default-int
c2653 'test' : is not a class or namespace name
'test' : function should return a value; 'void' return type assumed
what should I do?
You have to define return type explicitly and return a value of that type if your function returns not void. Otherwise you will get that error.
class A
{
int int_func();
void void_func();
};
int A::int_func()
{
return 0;
}
void A::void_func()
{
// No return;
}
Related
I've been trying to pass my Graphics Manager class to both my Robot and Room class.
But when attempting to pass the class by reference I get 3 errors regarding the pass by reference.
These are the errors I'm referring to:
C2143 syntax error: missing ';' before '*'
C4430 missing type specifier - int assumed. Note: C++ does not support default-int
C2238 unexpected token(s) preceding ';'
I have attempted to change the way I've been passing the classes but with no luck, I have highlighted the areas in which is causing the error as well as the code that i have tried to use to fix the problem.
Any advice in how i could go about fixing these errors is highly appreciated.
I have not included the full .cpp files as they are quite large but I will include a link to a pasteBin with the full script.
GrapicsManager.h
#pragma once
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Room.h"
#include "Robot.h"
class GraphicsManager
{
public:
Room* room; //This does not Flag Up Errors
Robot* robot; //This does not Flag Up Errors
Robot.h
#pragma once
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/System/String.hpp>
#include "GraphicsManager.h"
//#include "Room.h" //This what i had
class Room; //This is what i changed
//class GraphicsManager; //Wasnt sure if i should use it this
//way
class Robot
{
public:
//Graphics Variables
Room* room; //This works after the change
Robot* robot; //This works after the change
GraphicsManager *gm; //This throughs up the error
//This Is what i attemped to use with no effect
//GraphicsManager* gm = new GraphicsManager(room, robot);
Robot.cpp https://pastebin.com/Xd1A3Vii
#include "Robot.h"
Robot::Robot()
{
gm = new GraphicsManager(room, robot); //This tells me gm is
//not declared
this->room = room; //This does not flag up errors
this->robot = robot; //This does not flag up errors
//Room &room = *rm; // attempted to use this but decided not
//to
}
Room.h
#pragma once
#include <SFML/Graphics.hpp>
#include <SFML/System/String.hpp>
#include "GraphicsManager.h" //
//#include "Robot.h" //what i orginally had
//class GraphicsManager; //i decided not to do it this way
class Robot; //What i changed it to
class Room
{
public:
//Reference to other classes
Room* room; //This doesnt throw errors
Robot* robot; //This doesnt throw errors
//Refference to graphics manager
GraphicsManager *gm; //This throws the three errors mentioned
};
Room.cpp https://pastebin.com/6R6vnVfy
#include "Room.h"
Room::Room()
{
gm = new GraphicsManager(room, robot);
this->room = room;
this->robot = robot;
It's the classic cicular include issue. GrapicsManager.h includes Room.h and Robot.h which each include GrapicsManager.h again. Now, for example, when compiling GraphicsManager.cpp you include GrapicsManager.h. But before you ever get to the GraphicsManager class definition, you first include Room.h. From there you go straight to include GrapicsManager.h again, but since you have a #pragma once in there, the compiler will simply skip that include. By the time the compiler then gets to the GraphicsManager *gm; member declaration in Room.h, is has never seen a declaration of a type named GraphicsManager. The error message that Visual C++ gives you then
C4430 missing type specifier - int assumed. Note: C++ does not support default-int
is arguably a bit unintuitive. At the point where it encounters the identifier GraphicsManager, an identifier can only mean the start of a declaration. Since GraphicsManager is not a known type, the compiler assumes that identifier must be the name of the entity that is supposed to be declared and you just forgot to specify the type. That's why you get the error message you see. C in the olden days used to allow you to omit the type specifier in a declaration which would just mean to use int as a default. So you would see this error as a result of trying to compile ancient, non-standard C code. That's why the error message contains the explicit note that that's not allowed…
You already added forward declarations for Room in Robot.h and for Robot in Room.h. You'll have to do the same for GraphicsManager…
I am trying to migrate some code written in an older version of Visual Studio (VS 2006) to the 2015 version, and I stumbled upon an error in multiple files.
#ifndef C_I3E_TYPE_ARRAY_H_
#define C_I3E_TYPE_ARRAY_H_
#include "C_I3E_Type.h"
class C_I3E_Type_Array:public C_I3E_Type {
protected:
virtual void Read(FILE *p_Stream);
unsigned int m_High_Bound;}
It's about the Read method.
#include "StdAfx.h"
#include "C_I3E_Type_Array.h"
#include "C_I3E_File.h"
#include "C_I3E_Module.h"
C_I3E_Type_Array::Read(FILE *p_Stream){
unsigned int linked_type_index;
//Get the Type Index
linked_type_index = Read_Numeric_Format(p_Stream);
m_Linked_Type = m_Parent->Get_Type_ByIndex(linked_type_index);
//Get the High Bound value of the array
m_High_Bound = Read_Numeric_Format(p_Stream);}
It keeps sending me this error :
Severity Code Description Project File Line Suppression State
Error C4430 missing type specifier - int assumed. Note: C++ does not
support default-int MaskGen
d:\temp\bll\maskgen_whole_wo_dll\maskgen_all_classes_enabled\maskgen\classes\c_i3e\C_I3E_Type_Array.h 9
The error is quite descriptive, consider the line bellow:
C_I3E_Type_Array::Read(FILE *p_Stream) {
In the header file, there's a void there. So it should be:
void C_I3E_Type_Array::Read(FILE *p_Stream) {
#include "C_I3E_File.h" in the header. Add void before Read()'s definition.
This question already has answers here:
Why is there no call to the constructor? [duplicate]
(3 answers)
Closed 8 years ago.
I am using Code::Blocks 10.05 with GCC on Windows 7. I was experimenting with C++ constructors and I compiled and executed the following program.
#include<iostream>
using namespace std;
class base {
public:
base() {
cout<<"\n Constructor Invoked\n";
}
};
int main() {
base ob;
return 0;
}
The output was as expected and shown below.
Constructor Invoked
But while typing the program I accidentally compiled the following program. To my surprise it compiled without any error or warning.
#include<iostream>
using namespace std;
class base {
public:
base() {
cout<<"\n Constructor Invoked\n";
}
};
int main() {
base ob();
return 0;
}
But the program didn't give any output, just a blank screen. But no error or warning. Since it hasn't called the constructor I assume no object was created. But why no error or warning? Am I missing something very obvious?
When I added the line cout<<sizeof(ob); I got the following error message.
error: ISO C++ forbids applying 'sizeof' to an expression of function type
So what is ob? Is it considered as a function or an object?
Please somebody explain the line of code base ob(); and what actually happens in the memory when that line of code is executed?
Thank you.
You have declared a function with
base ob();
It will do nothing.
See here
I've tried compiling this with Visual Studio 2012 RC and Intel C++ Compiler XE 12.1. I'd appreciate if you tried with some other compiler. See my comments in the code to really appreciate the weirdness of this bug. Does anyone know what's going on, and where should I file a bug report about this?
// File: NamedSameA.h
#pragma once
// File: NamedSameA.cpp
#include <vector>
#include "NamedSameA.h"
struct NamedSame // Rename this class to something else to make the program work
{
std::vector<int> data;
// Comment out the previous line or change
// the data type to int to make the program work
};
static NamedSame g_data; // Comment out this line to make the program work
// File: NamedSameB.h
#pragma once
void test();
// File: NamedSameB.cpp
#include <vector>
#include "NamedSameA.h"
#include "NamedSameB.h"
struct NamedSame
{
int data1; // Comment out this line to make the program work
std::vector<int> data2;
};
void test()
{
NamedSame namedSame;
namedSame.data2.assign(100, 42);
// The previous line produces the following runtime error:
// -------------------------------------------------------
// Debug Assertion Failed!
// Program: C:\Windows\system32\MSVCP110D.dll
// File: c:\program files (x86)\microsoft visual studio 11.0\vc\include\vector
// Line: 240
// Expression: vector iterators incompatible
}
By giving the same name to two different classes/structures you've violated the One Definition Rule. This results in undefined behavior, so any result is possible - including a crash.
I've found over the years that the more convinced I am that I've found a compiler bug, the more likely it is that my program has a fundamental flaw.
Maybe I missed something, but I cant figure out why Visual Studio 2008 isn't seeing the rdbuf() procedure. Here is my code:
16. #include "DebugBuffer/BufferedStringBuf.h"
17.
18. BufferedStringBuf debug_buffer(256);
19. std::cout.rdbuf(&debug_buffer);
The BufferedStringBuf class is from this page: http://www.devmaster.net/forums/showthread.php?t=7037
Which produces the following error:
...src\main.cpp(19) : error C2143: syntax error : missing ';' before '.'
All I want to do is redirect std::cout to the Visual Studio Output window using OutputDebugString()..
You're not allowed to have executable statements at file-level scope. You can declare variables, but you can't call functions as standalone statements. Move your code into a function (such as gf's answer demonstrates), and you should have no problem.
Using the example class given on that site i don't have any problem:
#include <iostream>
#include "DebugBuffer/BufferedStringBuf.h"
class DbgBuf : public BufferedStringBuf {
public:
DbgBuf() : BufferedStringBuf(255) {}
virtual void writeString(const std::string &str) {}
};
int main()
{
DbgBuf debug_buffer;
std::cout.rdbuf(&debug_buffer);
}
Note that you have to create an instance of a class that derives from BufferedStringBuf because BufferedStringBuf::writeString() is pure virtual, thus making it an abstract class - abstract classes can't be instantiated.