Consider the following code:
Automobile.h
class Automobile
{
static string m_stCityCode;
static bool CheckCityCode(const Automobile& obj);
};
Automobile.cpp
bool Automobile::CheckCityCode(const Automobile& obj)
{
return m_stCityCode == obj.m_stCityCode;
}
int main()
{
//do something
}
I get the following error
"Severity Code Description Project File Line Suppression State
Error LNK2001 unresolved external symbol "public: static class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char> > Automobile::m_stCityCode"
(?m_stCityCode#Automobile##2V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##A) myPro C:\Users\zhivko.rusev\Documents\Visual
Studio 2015\Projects\myPro\myPro\Calls.obj 1 "
I would appreciate every help for solving this problem. Thanks in advance!
The static member needs to be defined. The error message is the linker's way of telling you it isn't. Your code declares the static member but does not define it.
To define it, in a single compilation unit (i.e. a non-header source file) simply add a line at file scope after including the header file
#include "Automobile.h"
std::string Automobile::m_stCityCode = ""; // change the initialiser to whatever suits
Do this in exactly one compilation unit. One is necessary to define the symbols. Multiple definitions (e.g. in multiple source files within your project) will cause the linker to complain about symbols being defined multiple times.
There are other problems in your code as shown, beyond what you have asked about, but I'll assume that just reflects you having left information out.
Related
I am porting a library from AS3 to Haxe and I need to make a method accepting variable number of arguments. Target is a *.swc library.
My question relates to this one but none of the suggested solutions outputs a method with the required signature: someMethod(...params)
Instead, the produced method is: someMethod(params:*=null)
This won't compile in AS3 projects using the library and the used code is beyond my reach. Is there a way to do this, perhaps macros?
Well, that's a great question. And, it turns out there is a way to do it!
Basically, __arguments__ is a special identifier on the Flash target, mostly used to access the special local variable arguments. But it can also be used in the method signature, in which case it changes the output from test(args: *) to test(...__arguments__).
A quick example (live on Try Haxe):
class Test {
static function test(__arguments__:Array<Int>)
{
return 'arguments were: ${__arguments__.join(", ")}';
}
static function main():Void
{
// the haxe typed way
trace(test([1]));
trace(test([1,2]));
trace(test([1,2,3]));
// using varargs within haxe code as well
// actually, just `var testm:Dynamic = test` would have worked, but let's not add more hacks here
var testm = Reflect.makeVarArgs(cast test); // cast needed because Array<Int> != Array<Dynamic>
trace(testm([1]));
trace(testm([1,2]));
trace(testm([1,2,3]));
}
}
Most importantly, this generates the following:
static protected function test(...__arguments__) : String {
return "arguments were: " + __arguments__.join(", ");
}
I'm trying to update a small Flex AS3 "project" consisting of one main file and an imported AS3 class. Unfortunately during compile I get the error 1120:Access of undefined property DEBUG. and the compilation fails. I've used mxmlc from Flex SDK 4.6 and Flash Builder 4.5 and get the same failure.
Flex isn't my strong suit so I hope someone can point out the error. From what I understand this source code compiled fine in 2011 using mxmlc.
Relevant code from the imported file:
package {
public class krpano_as3_interface {
public static var instance:krpano_as3_interface = null;
.
.
static public const STARTDEBUGMODE : int = 0xFF;
static public const DEBUG : int = 0;
And From the main AS3 file:
package {
.
import krpano_as3_interface;
public class soundinterface extends Sprite {
static public var krpano : krpano_as3_interface = null;
.
public function soundinterface() {
if (stage == null){
}else{
txt.htmlText = "krpano " + DEBUG::version + "\n\n" +
"<b>soundinterface plugin</b>" +
"\n\n(build " + DEBUG::builddate + ")";
}
}
If I rename or move the imported file the compiler complains that it is missing. The class where the constant DEBUG is defined should be being imported so why isn't it working?
The class where the constant DEBUG is defined should be being imported so why isn't it working?
Because they have nothing to do with each other.
DEBUG::version
and
static public const DEBUG : int = 0;
Are two unrelated parts of your code.
There are two hints in the syntax:
the :: name qualifier operator stands after a namespace, so whatever DEBUG is, it is a namespace, which the public static const is not (it's an int)
A property version is accessed. The public static const does not
have such a property.
What you are looking at is conditional compilation, which (among other things) allows you to specify values and pass them to the compiler to perform the compilation process.
You can also pass Strings and Numbers to the application and use them as inline constants
In your case, you want to define a version constant in the compiler arguments. Something like this:
-define+=DEBUG::version,"5"
This is probably because the version number is maintained by some build script (make, ant, whatever) and therefore passes this information to the compiler.
I highly recommend that you get in contact with the developer who worked on this project before to understand how the build process of this project is supposed to work.
I have a struct, file, and a class with functions in it. I'm trying to create a linked list of structs in my function, but I can't quite understand how to go about doing it! Here's my work thus far for that portion of my code:
#include <iostream>
using namespace std;
class myPrintSpool
{
public:
void send(string);
private:
int printSpoolSize;
myPrintSpool *printSpoolHead;
myPrintSpool* next;
};
struct file
{
string fileName;
int filePriority;
file* next;
};
void myPrintSpool::send(string name)
{
//Adds to linked list
file file;
myPrintSpool* file = new myPrintSpool;
if(printSpoolHead == NULL)
{
printSpoolHead = file;
}
else
{
file->next = printSpoolHead;
printSpoolHead = file;
}
printSpoolSize++;
}
I send a string inside the function so that when the user inputs a file name to send, it changes the name of the struct to that fileName in struct file changes to that input name. However, I'm not sure what to do since I cannot get the above portion to work properly first.
Thank you in advance, and thank you for taking the time to help me out!
It's been a while since I've done any C++, so this is all from recollection. In the code you supplied, you aren't instantiating a class. You are allocating memory for one.
If you want to assign a new class to that memory:
myPrintSpool printSpool = new myPrintSpool();
myPrintSpool *file = &printSpool;
I'm somewhat confused as to what you are actually doing. It seems the printSpoolHead will always be equal to the current myPrintSpool object, and if it's anything other than the first instantiation, it points back to itself.
As you didn't say anything specific as to the location of your error(s), do a simple output to verify the function is doing what you think it should (or is) doing.
Edit: Actually, I recall being thrown off by C++ instantiation, so it may be:
myPrintSpool printSpool;
I've got a string which, in run-time, contains the name of a class that I want to instantiate. How would I do that?
I read suggestions to use flash.utils.getDefinitionByName():
var myClass:Class = getDefinitionByName("package.className") as Class;
var myInstance:* = new myClass();
However, that gives me the following error:
[Fault] exception, information=ReferenceError: Error #1065: Variable className is not defined.
The easiest method I've come up with is to simply write the classnames out, separated by semicolons, anywhere in your project.
e.g. I create an Assets.as file with this in it:
package {
public class Assets {
// To avoid errors from the compiler when calling getDefinitionByName
// just list all of the classes that are not otherwise referenced in code:
Balloon;
Cloud;
FlyingHorse;
FlyingPig;
UFO;
Zeppelin;
}
}
Full code example/tutorial on this is here: http://producerism.com/blog/flashpunk-dame-and-lua-tutorial-part-6/
The other option is to use the mxmlc -includes compiler argument like this:
-includes=com.mydomain.package.MyClass
http://blogs.adobe.com/cantrell/archives/2010/09/loading-classes-dynamically-in-actionscript-3.html
I have this class defined in Global.h
class Global
{
public:
static string InttoStr(int num);
};
In Global.cpp, i have
Global::InttoStr(int num)
{
//Code To convert integer into string.
}
Now, from SubMove.cpp, when i call Global::InttoStr(num) I get following error:
error LNK2019: unresolved external symbol Global::InttoStr(int) referenced in function SubMove::toString(void)
Then I made the function non-static and called it like so:
Global g;
g.InttoStr(num);
But the error still persists.
I thought it had something to do with extern and searched it but i could not make any connection. Please help.
First off, try this:
string Global::InttoStr(int num)
{
//Code To convert integer into string.
}
Also, are you calling InttoStr from another library ? If so, you'll need to export the class "Global".
Best practice is to use a lib header (in the example below replace LIB_ with the name of the library):
#ifndef SOME_LIB_HEADER
#define SOME_LIB_HEADER
#if defined (LIB_EXPORTS)
#define LIB_API __declspec(dllexport)
#else
#define LIB_API __declspec(dllimport)
#endif // SOME_LIB_HEADER
Define LIB_EXPORTS in your project contianing Global, include the lib header in Global.h, then define the class like this
class LIB_API Global
{
// some code for the class definition
};
Each project should have its own LIB_EXPORTS and LIB_API definition like DLL1_EXPORTS, DLL1_API, DLL2_EXPORTS, DLL2_API etc.
Basically this makes a separate lib treat the previous dll with __declspec(dllimport) and resolve all externs.