Register - Login
Views: 99871231
Main - Memberlist - Active users - Calendar - Wiki - IRC Chat - Online users
Ranks - Rules/FAQ - Stats - Latest Posts - Color Chart - Smilies
05-04-22 07:07:44 PM
Jul - Computers and Technology - C++ question New poll - New thread - New reply
Next newer thread | Next older thread
Ninji

Birdo
Why did my user title say I'm a toaster anyway
Level: 88


Posts: 141/2014
EXP: 6639499
For next: 11165

Since: 07-26-07

Pronouns: he/him or they/them
From: Glasgow, Scotland

Since last post: 115 days
Last activity: 7 days

Posted on 04-15-08 06:14:44 PM Link | Quote
Repost from board2.

Okay, this is going to be weird, but.

Why can't I define a class and make a variable of it in another file? It makes no sense. It should be possible, but I don't know how. I'm using Visual Studio 2008.

This is basically what I have:

Main.cpp
+ Defining this: Tileset** Tilesets; (to make a pointer to an array of pointers to Tileset classes - this line is where all errors occur)
+ Main function and other things

Graphics.cpp
+ Definition of tileset class

Why does the compiler act as if I have no Tileset class? Moving the Tileset class definition into Main.cpp, before the problematic line, fixes it.
Is there something I can do to fix this? I'm kind of new to coding in C++.

____________________
neotransotaku
Member
wonders why OSX does not come with their version of MSPaint?
Level: 53


Posts: 246/603
EXP: 1084846
For next: 72273

Since: 08-24-07

From: The Landmark @ One Market

Since last post: 2.6 years
Last activity: 42 days

Posted on 04-15-08 08:22:51 PM Link | Quote
Class declarations should be in .h files.
Class definitions should be in .cpp files.

So, if you

1. Using what you have in graphics.cpp, make declarations of that class, such as your Tileset class in graphics.h.


class Tileset {

public:
Tileset() // constructor
//Whatever methods you want to have
private;
int foobar;
char quux;
// Whatever other private members you have
}



2. Include graphics.h in your graphics.cpp. This should be the ONLY change you need to that file.
3. Include graphics.h in your main.cpp. This should be the ONLY change you need to that file.
Ninji

Birdo
Why did my user title say I'm a toaster anyway
Level: 88


Posts: 142/2014
EXP: 6639499
For next: 11165

Since: 07-26-07

Pronouns: he/him or they/them
From: Glasgow, Scotland

Since last post: 115 days
Last activity: 7 days

Posted on 04-15-08 08:25:59 PM Link | Quote
Originally posted by neotransotaku
Class declarations should be in .h files.
Class definitions should be in .cpp files.

So, if you

1. Using what you have in graphics.cpp, make declarations of that class, such as your Tileset class in graphics.h.


class Tileset {

public:
Tileset() // constructor
//Whatever methods you want to have
private;
int foobar;
char quux;
// Whatever other private members you have
}



2. Include graphics.h in your graphics.cpp. This should be the ONLY change you need to that file.
3. Include graphics.h in your main.cpp. This should be the ONLY change you need to that file.

Do I really need to use .h files? I don't really like them, coming from a C# background - I'd rather keep my class methods and declaration in the same place if possible.

____________________
neotransotaku
Member
wonders why OSX does not come with their version of MSPaint?
Level: 53


Posts: 249/603
EXP: 1084846
For next: 72273

Since: 08-24-07

From: The Landmark @ One Market

Since last post: 2.6 years
Last activity: 42 days

Posted on 04-15-08 09:46:18 PM (last edited by neotransotaku at 04-15-08 06:46 PM) Link | Quote
C# has no relation to C/C++. How you build and organize code in C# is completely different from the model used in C/C++. The methodology I gave you is best practice because of the nature of the compiler and linker used to put everything together. In order for a compiler to build a C/C++ object file, all type references need to be resolved before compiling can begin. The compiler in C# is able to handle code the way you wrote in this thread because it is able to look later to find what it needs.
GuyPerfect
Catgirl
Level: 68


Posts: 390/1096
EXP: 2666043
For next: 62757

Since: 07-23-07


Since last post: 1.7 years
Last activity: 221 days

Posted on 04-17-08 01:05:17 AM Link | Quote
You really do need to use header files. Technically, the compiler only looks at one file at a time.

• If one file uses a function defined in another file, the compiler doesn't care. All the compiler needs to know is what kinds of arguments the function takes and what its return type is. That's all it needs to know when you compile.

• When the linker does its job, it matches up the function calls from the first file to the actual function definition in the other. All this means is that you need to have all files together at time of build; but not at time of compile.

It's kinda strange to grasp coming from one of Microsoft's chunkier development environments, but C/C++ "projects" aren't really projects so much as simple file lists that tell the compiler what it needs to successfully build the executable.
Ninji

Birdo
Why did my user title say I'm a toaster anyway
Level: 88


Posts: 143/2014
EXP: 6639499
For next: 11165

Since: 07-26-07

Pronouns: he/him or they/them
From: Glasgow, Scotland

Since last post: 115 days
Last activity: 7 days

Posted on 04-17-08 08:54:42 PM Link | Quote
Now I'm getting assaulted by C2784. I have no idea what I'm doing wrong. ._.

My code: http://pastebin.com/m49b163a8
Huge error log: http://pastebin.com/m23be1b8d
What does this mean?

____________________
Kagome
Member
Level: 25


Posts: 33/113
EXP: 87744
For next: 1876

Since: 09-24-07


Since last post: 10.0 years
Last activity: 9.6 years

Posted on 04-17-08 08:56:33 PM Link | Quote
The error log doesn't show which line just failed?
GuyPerfect
Catgirl
Level: 68


Posts: 391/1096
EXP: 2666043
For next: 62757

Since: 07-23-07


Since last post: 1.7 years
Last activity: 221 days

Posted on 04-17-08 09:00:54 PM Link | Quote
I usually work in C, so my header file memory is a bit lacking, but... Don't you need iostream.h to use cout, <<, etc.?
Joe
Common spammer
🍬
Level: 111


Posts: 420/3392
EXP: 14503183
For next: 365177

Since: 08-02-07

From: Pororoca

Since last post: 13 days
Last activity: 7 min.

Posted on 04-17-08 09:10:39 PM Link | Quote
Since this is C++, it would just be <iostream>.

____________________
らき☆すた
Lucky☆Star
neotransotaku
Member
wonders why OSX does not come with their version of MSPaint?
Level: 53


Posts: 252/603
EXP: 1084846
For next: 72273

Since: 08-24-07

From: The Landmark @ One Market

Since last post: 2.6 years
Last activity: 42 days

Posted on 04-17-08 09:28:44 PM Link | Quote



string* RetVal = new string[FileList.size()];
list<string>::iterator it;
int ct = 0;
for (it = FileList.begin() it < FileList.end() ++it) {
RetVal[ct] = *it;
ct++;
}



It doesn't like the way you are using the iterator at line 126. It is complaining that it can't deduce the correct types from the generic list you are using. I'm not that familiar with generics in C++, so I can't help you more than that. Are you sure that is the correct syntax when using an iterator in C++? If so, perhaps you need to turn on certain compiler flags? *shrugs*
Ninji

Birdo
Why did my user title say I'm a toaster anyway
Level: 88


Posts: 144/2014
EXP: 6639499
For next: 11165

Since: 07-26-07

Pronouns: he/him or they/them
From: Glasgow, Scotland

Since last post: 115 days
Last activity: 7 days

Posted on 04-17-08 09:39:06 PM Link | Quote
Originally posted by GuyPerfect
I usually work in C, so my header file memory is a bit lacking, but... Don't you need iostream.h to use cout, <<, etc.?

It's included in header.h, a file I include into all my .cpp files.

Originally posted by Kagome
The error log doesn't show which line just failed?

It's line 126 -- the start of the for loop in ListFiles().

Originally posted by neotransotaku

	string* RetVal = new string[FileList.size()];

list<string>::iterator it;
int ct = 0;
for (it = FileList.begin() it < FileList.end() ++it) {
RetVal[ct] = *it;
ct++;
}



It doesn't like the way you are using the iterator at line 126. It is complaining that it can't deduce the correct types from the generic list you are using. I'm not that familiar with generics in C++, so I can't help you more than that. Are you sure that is the correct syntax when using an iterator in C++? If so, perhaps you need to turn on certain compiler flags? *shrugs*

This is what I'm looking at: http://www.cplusplus.com/reference/stl/list/begin.html


____________________
neotransotaku
Member
wonders why OSX does not come with their version of MSPaint?
Level: 53


Posts: 253/603
EXP: 1084846
For next: 72273

Since: 08-24-07

From: The Landmark @ One Market

Since last post: 2.6 years
Last activity: 42 days

Posted on 04-18-08 01:11:28 AM Link | Quote
Probably the operator of < is undefined for what you are doing. Like the other thread, you should probably use !=. The speed of < and != is the same.
Next newer thread | Next older thread
Jul - Computers and Technology - C++ question New poll - New thread - New reply


Rusted Logic

Acmlmboard - commit 47be4dc [2021-08-23]
©2000-2022 Acmlm, Xkeeper, Kaito Sinclaire, et al.

32 database queries, 7 query cache hits.
Query execution time: 0.104199 seconds
Script execution time: 0.017181 seconds
Total render time: 0.121381 seconds