In my app delegate class I define a constant like so:
#define kSomeConstant #"My_Constant_Value"
I then want to make use of this constant in another viewController so I defined it again exactly as above. I now get the message:
'kSomeConstant' macro redefined
Why is this the case? Is there some other way I can access the constant?
Simply redefining the constant (ie : same constant name). Choose another name, or define it just one time in a header file to access the constant value every where you import that header.
It's likely been defined twice among the sources which are visible to the compiler. the compiler may tell you where the previous definition is if you look into the message in more detail.
To avoid this problem entirely, declare your NSString constants like this:
file.h - declaration:
extern NSString* const kSomeConstant;
file.m/mm - definition:
NSString* const kSomeConstant = #"My_Constant_Value";
Then when you need to use the constant, just #include "file.h".
The compiler has encountered two definitions of the macro. Probably you defined it in a header file and then defined it again in a .m file.
If you can't figure out where the two definitions come from, try preprocessing the file with the error (in Xcode 4.2 this is under generate output in the product menu). You can then do a search for kSomeConstant and that should tell you where it comes from.
Related
I'm new to C++ and am exploring the use of timezones in chrono, specifically zoned_time.
I have only one issue with it: I want to use an instance of zoned_time as a member in a class, but no matter how I alter the syntax, the compiler doesn't like it.
#include <chrono>
#pragma once
class Event
{
private:
std::chrono::zoned_time tz;
public:
Event();
};
I've created a header file containing the code above, but it says the argument list for the class template is missing. I assume that means it wants me to initialize it, but I don't want to because it's a header file. As I said, I messed around quite a bit with the syntax of tz, combined with a good bit of research. Unfortunately, since I'm new to the language I really don't know what to look for online.
So my goal here is to be able to create a typed container within Event which will store the zoned_time information. I'm not picky on how I achieve that goal (I suspect some black-magic pointer trickery will be the solution). Thank you for the input.
Your error message mentions the word "template", indicating it's not about constructor arguments (initializer), but about generic type arguments (template parameters).
According to https://en.cppreference.com/w/cpp/chrono/zoned_time this class is declared as:
template <
class Duration,
class TimeZonePtr = const std::chrono::time_zone*
> class zoned_time;
So it seems like you need a duration type to use (there is a default for the time zone pointer type). So your line should probably say something like
std::chrono::zoned_time<std::chrono::seconds> tz;
I created a Swift enum as an Int (so it will work with Objective-c).
When I build the project, everything is fine. However, as soon as I try to import the class in a .h file (using a forward declaration #ClassName), the generated header file errors out with Redefinition of 'ClassName' as a different kind of symbol
Looking in the generated .h file I can see it generated the new type like so:
typedef SWIFT_ENUM(NSInteger, ClassName, closed) {
type1 = 1,
type2 = 2,
};
It underlines the ClassName saying it's been re-declared. However, it exists nowhere else in the header file (doing a search and it comes up only once) and it's only declared once in a swift file.
Any suggestions on what is going on?
Enums are not classes in Objective-C, so you need to use typedef instead of forward declaration in your .h file:
typedef NS_ENUM(NSInteger, ClassName);
and then in .m file, you need to import Module-Swift.h file.
I'm converting some code from Objective C to Swift so I can get a handle on Swift.
Basically, in Objective C, I had a header file containing some global constants which were accessible in other Objective C classes.
I changed my .h file to be .swift and changed the constants to look like:
public let INVALID_INTEGER_VALUE = (-32768)
public let INVALID_LONGLONG_VALUE = (0x8000000000000000)
...
But when I try to access these in another Swift file, the compiler gives "Use of unresolved identifier..." I tried importing my .swift file, but that also didn't work (as far as I can tell, I shouldn't need to import any of my swift files as they are part of the same module).
Any thoughts?
You could also run into this if the file that is trying to use the constant has been added to another target (like the tests target) that the file containing the constants has not been added to.
I was facing the same error as asked in this question
I overcome with this error by solution of declaring class ahead of time in my .h file with the class parameter
I am having FFTBufferManager.h and FFTBufferManager.cpp file and using it in HomeView.h and HomeView.mm file
class FFTBufferManager,CAStreamBasicDescription,DCRejectionFilter;
But now I am having error as
#include "FFTBufferManager.h"
#include "aurio_helper.h"
#include "CAStreamBasicDescription.h"
class CAStreamBasicDescription,FFTBufferManager; //here it shows this error
EXpected Unqualified-id befor ',' token
#interface HomeView
{
FFTBufferManager* fftBufferManager;
//it shows erros
EXpected Unqualified-id befor ',' token
ISO c++ forbids declaration of FFTBufferManager with no type
}
#property FFTBufferManager* fftBufferManager;
//shows error
'FFTBufferManager' is not a type
I'm gathering you're using both C++ and Objective-C.
I'd suggest renaming all your .cpp and .m files in which Objective-C and C++ code are meeting to use the extension .mm - this tells the compiler to use "Objective-C++" rules, and will stop a lot of compiler troubles.
Also, it seems CAStreamBasicDescritpion is a C++ class - you'll have to forward-declare it with class CAStreamBasicDescritpion;, not #class CAStreamBasicDescritpion; (note, no "at" sign) - the second form is only for forward-declaring Objective-C classes. This I suspect is the root cause of the particular error you have observed.
EDIT in response to comment: I'm not sure about your first new issue - that should work fine so long as both FFTBufferManager and CAStreamBasicDescription are C++ classes. As to your second one, depending on where exactly that line of code is (CAStreamBasicDescription thruFormat;) you may need to include the header rather than just the forward-declare: you're declaring an instance of CAStreamBasicDescription here, and the compiler needs to know its structure to do so.
You can't declare more than one class at a time.
Change your declarations to
class CAStreamBasicDescription;
class FFTBufferManager;
The compiler is looking for an unqualified-id because it believes that you're declaring a variable of type CAStreamBasicDescription, so it expects a variable name where you gave it a comma.
Looks like you are trying to create a class that already exists in one of the Cocoa frameworks.
Following is a snippet from a View Controller's implementation:
- (void)myOtherAwesomeMethod
{
[self myAwesomeMethod]; // Compile ERROR here: Receiver type for instance message does not declare a method with selector
}
- (void)myAwesomeMethod
{
NSLog(#"%#", #"Calling my awesome method...");
}
- (void)viewDidLoad
{
[self myAwesomeMethod];
[self myOtherAwesomeMethod];
}
I do not have myAwesomeMethod method declared in my header file, but why is it that I can call myAwesomeMethod in viewDidLoad, but not in myOtherAwesomeMethod?
I know the solution to this error is to declare the method in my header file, but I'd like to understand why this is happening.
In C, the rule is: you got to declare it before you use it.
Files are compiled from the top down. So here's what is happening in your code:
The compiler reads the #interface declaration for your class. Since you say neither method is declared in the header file, neither are in the symbol table yet.
Your symbol table contains: nothing yet
The compiler reads the method definition for myAwesomeMethod. You haven't declared it yet, so it gets added to the symbol table. The body of the method contains a call to NSLog, which was declared long ago in a header provided to you by Apple.
Your symbol table contains: myAwesomeMethod
The compiler reads the method definition for viewDidLoad; it was actually declared in a superclass's header file. The body of the method contains a call to myAwesomeMethod, which was found! It also contains a call to myOtherAwesomeMethod. Never heard of it!
Now, this isn't an error, because it can still generate the code to make the call. It can infer argument types by how you use it (in this case, no arguments). It can't be sure of the return type, though (just because you don't use the return value doesn't mean there wasn't one). So it proceeds on the assumption that the call returns id and generates a warning.
Your symbol table contains: myAwesomeMethod
Finally, the compiler reads the method definition for myOtherAwesomeMethod. It gets added to the symbol table. It compiles the body, which contains a call to myAwesomeMethod, which is in the table. All's well that ends well.
At the end of the file, your symbol table contains: myAwesomeMethod, myOtherAwesomeMethod
If you're coming from a language like Java, this may seem silly, but that's because Java works differently than C. In Java, source code is compiled and linked in one step; you can't compile a class that refers to another class without having either the source code or class files available. That can be a hassle, but on the other hand, you never need about declarations apart from definitions.
In C, compiling and linking are different steps. The compiler just generates object code that refers to symbols that may be defined elsewhere; it uses declarations to make sure it generates the right code. The linker is responsible for matching up all those symbols with definitions in other libraries, static or dynamic.
In Objective-C, the linker doesn't actually know/care about Objective-C messages, because there is no way of knowing at compile-time/link-time whether an object can respond to a message. So it passes the buck to the runtime, which throws an exception if you get that far without a method definition.
I admit I'm not sure why it works in viewDidLoad.
Notice that the error says "did not DECLARE" a method. That's a hint that it's looking for a declaration (typically in a header)..ie a function prototype.
However, there's nothing that says a declaration has to be in a header. YOu can put it at the top of the implementation file if you're not going to call that method from outside the file.